What is the best way to copy specific files from a list into a new directory using python?
For example, I have a text document containing file names like the below example:
E3004
D0402
B9404
C6089
I would like to search a directory and copy the files that are found to exist into a new directory whilst listing the codes that are not found into a new text document.
I am a complete python novice, so any help is much appreciated.
Here's a piece of code from a previous discussion which was put together as a solution to a similar problem, however, I am having trouble understanding where exactly to place my file paths for the src, dst and text document? Furthermore, is there a way to save out the data which was not found to a separate text document?
Link to the previous discussion: https://stackoverflow.com/a/51621897/10580480
import os import shutil from tkinter import * from tkinter import filedialog
root = Tk()
root.withdraw()
filePath = filedialog.askopenfilename()
folderPath = filedialog.askdirectory()
destination = filedialog.askdirectory()
filesToFind = []
with open(filePath, "r") as fh:
for row in fh:
filesToFind.append(row.strip())
filename variable itself for filename in os.listdir(folderPath):
if filename in filesToFind:
filename = os.path.join(folderPath, filename)
shutil.copy(filename, destination)
else:
print("file does not exist: filename")
Thanks