我需要使用CSV文件中的文件名列表來查找和複製相應的文件。以下是代碼。我沒有收到任何錯誤,但以下內容不會產生任何結果(我已檢查並重新檢查了使用相應文件創建的示例列表)。任何想法,我搞砸了嗎?我感謝任何和所有的幫助。 (我使用的是最新版本的Python)使用CSV文件中的文件名列表來查找和使用Python複製文件
import os, shutil, csv
files_to_find = []
with open('C:\\pdfsearch.csv') as fh:
reader = csv.reader(fh)
files_to_find = list(reader)
for root, dirs, files in os.walk('C:\\mail'):
for _file in files:
if _file in files_to_find:
print ('Found file in: ') + str(root)
shutil.copy(os.path.abspath(root + '/' + _file), 'C:\\Matches')
你的'files_to_find'列表是什麼樣的?它有相對或絕對路徑嗎? –
csv目前只包含兩個單元格。 files_to_find列表正在打印爲[[''1003055716CBR201510.pdf'],['1003080516CBR201510.pdf']] –
我認爲您需要「for file_name in reader:files_to_find.append(file_name)」。 –