比较两个目录,然后删除不匹配项 (Python)
你好 stackoverflow 社区!求助,已经伤透了如何实施。
例如,有文件夹:“D:\left”和“C:\right”。
它们包含的内容:文件、包含文件的目录、子目录、包含文件的子目录。大多数内容是相同的,但“C:\right”中可能有“额外”内容(与“D:\left”的内容不匹配)。
如何比较“С:\right”中的内容(“С:\right”中的内容)和“D:\left”中不存在的内容,然后(“С:\right”中的额外内容)将其删除,以便文件夹“D:” \left' 和 'C:\right' 变得相同(在我们的例子中,我们不考虑大小、时间等 - 纯粹通过它们内容的名称)。
尝试像这样删除多余的:
difs = list(set(os.listdir('C:\right')) - set(os.listdir('D:\left')))
但这还不够,因为它不会将效果传播到子目录。
也像这样:
from dirsync import sync
sync('D:\left', 'C:\right', 'diff')
但是,我只对输出的一小部分感兴趣,并且我根本不清楚如何准确地删除该输出。
删除 'C:\right' 中的所有内容以从 0 复制到 'D:\left' 到 'C:\right' 不是解决方案。
我很确定解决方案是固定的:
os.walk
但我只是无法将其正确排列:(
提前非常感谢您的帮助,我为我的愚蠢表示歉意。
为了清楚起见,我附上屏幕截图
Hello stackoverflow community! Help, already broke his head how to implement.
There are, for example, folders: 'D:\left' and 'C:\right'.
They contain the contents: files, directories with files, subdirectories, subdirectories with files. Most of the content is the same, but there may be 'extra' content in 'C:\right' (not matching the content of 'D:\left').
How can I compare the content (what is in) 'С:\right', what is not in 'D:\left' and after that (extra in 'С:\right') delete it so that the folders 'D:\left' and ' C:\right' became identical (in our case, we do not look at the size, time, etc. - purely by the names of their contents).
Tried like this to remove the excess:
difs = list(set(os.listdir('C:\right')) - set(os.listdir('D:\left')))
But this is not enough, because it does not propagate the effect to subdirectories.
Also like this:
from dirsync import sync
sync('D:\left', 'C:\right', 'diff')
But, there I am only interested in a small part of the output, and how exactly to put this output under deletion is simply not clear to me.
Delete everything from 'C:\right' to copy from 0 to 'D:\left' to 'C:\right' is not a solution.
I'm pretty sure the solution is fixated on:
os.walk
But I just can't line it up right :(
Many thanks in advance for any help and I apologize for the stupidity.
I'm attaching screenshots for clarity
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
Path.rglob
< /a>:这里是一个例子:
现在您只需删除
difference
中的所有文件和目录即可。You can use
Path.rglob
:Here is an example:
Now you just need to delete all files and directories in
difference
.非常感谢 Riccardo Bucco 的回复。我做到了,现在看起来像这样:
Thank Riccardo Bucco very much for his reply. I did it and now it looks like this: