I am trying to write a script that loops through all the files within a folder and compares it to a list. If a file name in that folder matches with an item in the list then I want to save a copy of that file in another folder.
I have tried this so far - my code runs but no files are saved in the new folder. Is anyone able to tell me why it's not working?
import os
import shutil
import fnmatch
import csv
sample = open(...../Sample.csv','r')
reader = csv.reader(sample)
samplelist= []
for row in reader:
if row != " ":
samplelist.append(row)
source = '..... /My Files'
destination = '.../Sample'
for file in os.listdir(directory):
if file in samplelist:
shutil.copy(source,destination)
question from:
https://stackoverflow.com/questions/65872015/check-if-a-filename-is-in-a-list 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…