![](/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.