如何使用 python 测试文件夹中是否有新文件

发布于 2024-08-21 11:26:42 字数 227 浏览 3 评论 0原文

您将如何进行测试以查看两个文件夹是否包含相同的文件,然后能够仅操作新文件。

A = listdir('C:/')
B = listdir('D:/')

If A==B

...

我知道这可以用来测试目录是否不同,但有更好的方法吗? 如果 A 和 B 相同,但 B 比 A 多一个文件,我如何仅使用新文件?

谢谢,我希望我的问题不会令人困惑

How would you go about testing to see if 2 folders contain the same files, and then to be able to manipulate ONLY the file which is new.

A = listdir('C:/')
B = listdir('D:/')

If A==B

...

I know this could be used to test if directories are different but is there a better way?
And if A and B are the same, except B has one more file than A, how do i use just the new file?

Thank you, i hope my question isnt confusing

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝影如岚 2024-08-28 11:26:42

http://docs.python.org/library/filecmp.html

http://docs.python.org/library/filecmp.html#the-dircmp -类

import filecmp
compare = filecmp.dircmp( "C:/", "D:/" )
for f in compare.left_only:
    print "C: new", f
for f in compare.right_only:
    print "D: new", f

http://docs.python.org/library/filecmp.html

http://docs.python.org/library/filecmp.html#the-dircmp-class

import filecmp
compare = filecmp.dircmp( "C:/", "D:/" )
for f in compare.left_only:
    print "C: new", f
for f in compare.right_only:
    print "D: new", f
我们的影子 2024-08-28 11:26:42
A = set(os.listdir('C:\\'))
B = set(os.listdir('D:\\'))

print 'Files in A but not in B:', A - B
print 'Files in B but not in A:', B - A
A = set(os.listdir('C:\\'))
B = set(os.listdir('D:\\'))

print 'Files in A but not in B:', A - B
print 'Files in B but not in A:', B - A
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文