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.
解决方案
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)