![](/img/trans.png)
[英]Copy files from one folder to another with matching names in .txt file
[英]Copy files with specific text at the end of their (file) names from one folder to other folder
我在一个文件夹中有 23000 个文件,并且想使用 python 将名称以某些特定文本结尾的文件复制到其他文件夹。我使用了以下代码,虽然它运行成功但它不会将文件复制到目标文件夹中。
import os
import shutil
path = "D:/Snow_new/test"
outpath = "D:/Snow_new/testout"
for f in os.listdir(path):
if f.endswith("clip_2"):
shutil.copyfile(os.path.join(path, f), outpath)
您正在尝试一次又一次地将文件复制到单个文件名,因此要修复它,您必须为每个文件添加带有输出路径的文件名
import os
import shutil
path = "D:/Snow_new/test"
outpath = "D:/Snow_new/testout"
if not os.path.isdir(outpath):
os.mkdir(outpath)
else:
shutil.rmtree(outpath)
os.mkdir(outpath)
for f in os.listdir(path):
f2, ext = os.path.splitext(f)
if f2.endswith("clip_2"):
shutil.copyfile(os.path.join(path, f), os.path.join(outpath, f))
在运行上面的代码之前,确保你完全删除了D:/Snow_new/testout
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.