[英]How to copy files from one folder to multiple other folders using a dictionary
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?是否有某种方法可以使用它将文件复制到与字典配对的文件夹中?
There is no built-in method, but a simple for-loop and shutil will get the job done.没有内置方法,但是一个简单的 for 循环和 shutil 就可以完成工作。
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
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.对于复制,我们可以使用shutil
模块,它提出了一些高级文件方法。
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.如果您有两个单独的字典,第一个(带有两个数组)应该非常接近您正在搜索的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.