I have a dictionary of the whole file path and the path to it's folder destination in a dictionary. Is there some method to use it to copy the files into the folder their paired with in the dictionary?
CodePudding user response:
There is no built-in method, but a simple for-loop and shutil will get the job done.
import shutil
files = {"source/path": "move/to"} # your dict
for source, move in files.items():
shutil.copyfile(source, f"dir/{move}") # dir/ is the directory your paths are in
CodePudding user response:
I didn't really understand if you have two separate dictionnaries/arrays for the source and destination, but the setup are pretty similar.
I would parse each file, and for each one parse each directory to then apply a file copying operation.
For the copying we can use the shutil
module which propose some high-level file methods.
Assuming your setup is :
files = ["/path/to/first/file", "/path/to/second/file" ...]
directories = ["/path/to/first/destination/", "/path/to/second/destination/" ...]
The copy code would be :
for file in files :
for path in directories:
shutil.copyfile(file, path)
Similarely if you have a dictionnary declared like this
dictionnary = {"/file/path" : "/file/destination"}
The code could be :
for file, path in dictionnary :
shutil.copyfile(file, path)
If you have two separates dictionnaries the first one (with two arrays) should be pretty close to what you're searching.