简体   繁体   English

如何使用字典将文件从一个文件夹复制到多个其他文件夹

[英]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.

相关问题 如何使用python将多个文件夹中的多个文件复制到一个文件夹中? - How to copy multiple files from multiple folders into one folder using python? 如何将文件从一个文件夹复制到多个文件夹 - How can I copy files from a folder into multiple folders 如果字典中的标志为True,如何将文件从一个文件夹复制到另一个文件夹 - How to copy files from the one folder to another if flag is True in dictionary 将文件从一个文件夹复制到另一个文件夹(而不是覆盖) - Copy files from one folder to other (and not overwrite) 如何根据文件夹名称将文件从一个带有子文件夹的目录复制到另一个带有子文件夹的目录? - How do I copy files from one directory with sub-folders to another directory with sub-folders based on folder name? 如何使用 Python 将子文件夹和文件复制到新文件夹中 - How to Copy Sub Folders and files into New folder using Python Python:将文件从不同位置的多个文件夹移动到一个文件夹中 - Python: Move files from multiple folders in different locations into one folder 将多个文件从子文件夹复制到一个文件夹 - Copy multiple files from sub folder into one folder 如何使用 Python 复制多个文件夹中的多个文件 - How to copy multiple files in multiple folders using Python 如何将多个文件夹中的相似命名文件合并为每个文件名的一个文件夹 - How to combine similar named files from multiple folders into one folder for each file name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM