【问题标题】:How to test a folder for new files using python如何使用python测试文件夹中的新文件
【发布时间】:2010-02-12 12:29:15
【问题描述】:

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

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

If A==B

...

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

谢谢你,我希望我的问题没有混淆

【问题讨论】:

  • “新”是指存在于一个文件夹中而不存在于另一个文件夹中?
  • 是的,这就是我的意思,我正在尝试编写一个脚本,该脚本将通过网络将文件从一个文件夹发送到另一个文件夹,反之亦然

标签: python file directory


【解决方案1】:

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

【讨论】:

    【解决方案2】:
    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-09
      • 2016-01-27
      • 2021-05-13
      • 1970-01-01
      • 1970-01-01
      • 2014-06-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多