Python - copying specific files from a list into a new folder

前端 未结 2 1357
长情又很酷
长情又很酷 2021-01-15 11:02

I am trying to get my program to read a list of names from a file (say .txt), then search for those in a selected folder and copy and paste those files to another selected f

2条回答
  •  执念已碎
    2021-01-15 11:30

    Your last section has a problem.

    for file in os.listdir(folderPath): 
        for file in files:
            if file in filesToFind:
                shutil.copy(file, destination)
    

    The first for loops over each filename in the directory, which is perfectly understandable.

    The second for is an error, because files does not exist. What did you intend it to do?

提交回复
热议问题