Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
99 views
in Technique[技术] by (71.8m points)

python 3.x - Check if a filename is in a list

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...