【发布时间】:2015-05-18 05:57:25
【问题描述】:
我正在尝试使用os.walk() 模块遍历多个目录并将每个目录的内容移动到单个“文件夹”(dir)中。
在这个特定的示例中,我有数百个需要移动的 .txt 文件。我尝试使用shutil.move() 和os.rename(),但没有成功。
import os
import shutil
current_wkd = os.getcwd()
print(current_wkd)
# make sure that these directories exist
dir_src = current_wkd
dir_dst = '.../Merged/out'
for root, dir, files in os.walk(top=current_wkd):
for file in files:
if file.endswith(".txt"): #match files that match this extension
print(file)
#need to move files (1.txt, 2.txt, etc) to 'dir_dst'
#tried: shutil.move(file, dir_dst) = error
如果有办法移动目录的所有内容,我也会对如何做到这一点感兴趣。
非常感谢您的帮助!谢谢。
这里是文件目录和内容
current_wk == ".../Merged
在current_wk有:
Dir1
Dir2
Dir3..
combine.py # python script file to be executed
在每个目录中都有数百个.txt文件。
【问题讨论】:
标签: python python-3.x