使用 Python 将所有文件从一个目录移动到另一个目录 [英] Moving all files from one directory to another using Python
本文介绍了使用 Python 将所有文件从一个目录移动到另一个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用 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)
我希望它移动 Blob
文件夹中的所有文件.我没有收到错误,但它也没有移动文件.
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.
推荐答案
试试这个:
import shutil
import os
source_dir = '/path/to/source_folder'
target_dir = '/path/to/dest_folder'
file_names = os.listdir(source_dir)
for file_name in file_names:
shutil.move(os.path.join(source_dir, file_name), target_dir)
这篇关于使用 Python 将所有文件从一个目录移动到另一个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文