[英]Copy files with specific text at the end of their (file) names from one folder to other folder
I have 23000 files in one folder and want to copy files with their names ending with some specific text to other folder using python. I used the following code, though it runs successfully but it doesn't copy the file in the destination 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)
you are trying to copy files to a single filename again and again, so to fix it you will have to add the filename with outpath for every file您正在尝试一次又一次地将文件复制到单个文件名,因此要修复它,您必须为每个文件添加带有输出路径的文件名
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))
before running the above code make sure that you remove D:/Snow_new/testout
completly在运行上面的代码之前,确保你完全删除了
D:/Snow_new/testout
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.