简体   繁体   English

将文件复制到另一个文件夹,避免在 python 中重复

[英]copy files to another folder, avoiding duplication in python

I need your help.我需要你的帮助。 I'm a semi-beginner in python and I would like to create a program that allows me to copy the content (in this case photos) of a folder A to another folder B. However, the content of folder B may, potentially, already contain files from folder B .我是 python 的半初学者,我想创建一个程序,允许我将文件夹 A 的内容(在本例中为照片)复制到另一个文件夹 B。但是,文件夹 B 的内容可能潜在地,已经包含文件夹 B 中的文件

So I need to avoid either copying them or deleting the duplicates afterwards.所以我需要避免复制它们或之后删除重复项。 (The name is enough, there is no need to check the size etc.) I thought of lists which with the help of two nested "for" loops would detect identical names. (名称就足够了,不需要检查大小等。)我想到了在两个嵌套“for”循环的帮助下可以检测到相同名称的列表。 I don't think this is the smartest solution.我认为这不是最聪明的解决方案。

How would you translate this into python?您如何将其转换为 python?

Thanks a lot for your help.非常感谢你的帮助。

I found that on the web: https://gist.github.com/vinovator/a2ba7306e829bf3a9010我在 web 上发现: https://gist.github.com/vinovator/a2ba7306e829bf3

PS: My final goal is to create a box with a rasbperry connected to a HDD and a SD card port that will allow me, as soon as I insert the Sd card in the "slot" to automatically save new pictures. PS:我的最终目标是创建一个带有连接到 HDD 和 SD 卡端口的盒子,只要我将 Sd 卡插入“插槽”以自动保存新图片。

Hi get the path list of each folder and turn them into a set so you spot the difference between the two folders.您好,获取每个文件夹的path list并将它们转换为一set ,以便您发现两个文件夹之间的区别。

import glob

folderA = glob.glob("pathTofolderA")
folderB = glob.glob("pathTofolderB")

diffA = list(set(folderA) - set(folderB))
diffB = list(set(folderB) - set(folderA))

depending on the difference you are looking for your answer is diffA or diffB .根据您正在寻找答案的差异是diffAdiffB More information on set here: https://realpython.com/python-sets/#operating-on-a-set有关此处设置的更多信息: https://realpython.com/python-sets/#operating-on-a-set

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM