Moving all files from one directory to another using Python

Moving all files from one directory to another using Python



I want to move all text files from one folder to another folder using Python. I found this code:


import os, shutil, glob

dst = '/path/to/dir/Caches/com.apple.Safari/WebKitCache/Version 4/Blobs '
try:
os.makedirs(/path/to/dir/Tumblr/Uploads) # create destination directory, if needed (similar to mkdir -p)
except OSError:
# The directory already existed, nothing to do
pass

for txt_file in glob.iglob('*.txt'):
shutil.copy2(txt_file, dst)



I would want it to move all the files in the Blob folder. I am not getting an error, but it is also not moving the files.


Blob




4 Answers
4



Try this..


import shutil
import os

source = '/path/to/source_folder'
dest1 = '/path/to/dest_folder'


files = os.listdir(source)

for f in files:
shutil.move(source+f, dest1)






Note: The above will move files recursively from source to destination. Also, in my test, the above code is missing a trailing slash in both source and dest1.

– Dan Nissenbaum
May 1 '18 at 4:10


source


dest1






Instead of concatenating strings, it's better to use os.path.join() method

– Kuu
Oct 29 '18 at 14:28



os.path.join()



Copying the ".txt" file from one folder to another is very simple and question contains the logic. Only missing part is substituting with right information as below:


import os, shutil, glob

src_fldr = r"Source Folder/Directory path"; ## Edit this

dst_fldr = "Destiantion Folder/Directory path"; ## Edit this

try:
os.makedirs(dst_fldr); ## it creates the destination folder
except:
print "Folder already exist or some error";



below lines of code will copy the file with *.txt extension files from
src_fldr to dst_fldr


for txt_file in glob.glob(src_fldr+"\*.txt"):
shutil.copy2(txt_file, dst_fldr);



This should do the trick. Also read the documentation of the shutil module to choose the function that fits your needs (shutil.copy(), shutil.copy2(), shutil.copyfile() or shutil.move()).


import glob, os, shutil

source_dir = '/path/to/dir/with/files' #Path where your files are at the moment
dst = '/path/to/dir/for/new/files' #Path you want to move your files to
files = glob.iglob(os.path.join(source_dir, "*.txt"))
for file in files:
if os.path.isfile(file):
shutil.copy2(file, dst)






but where do i define the new destination for the txt files?

– malina
Jan 24 '17 at 11:31



Please, take a look at implementation of the copytree function which:



List directory files with:



names = os.listdir(src)


names = os.listdir(src)



Copy files with:



for name in names:
srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)
copy2(srcname, dstname)


for name in names:
srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)
copy2(srcname, dstname)



Getting dstname is not necessary, because if destination parameter specifies a directory, the file will be copied into dst using the base filename from srcname.



Replace copy2 by move.



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)