【问题标题】:Using python to move multiple files and delete duplicates使用python移动多个文件并删除重复项
【发布时间】:2020-10-09 19:17:57
【问题描述】:

除了操作系统,我还应该为此使用任何特定的库/模块吗?我是初学者。

我有多个目录,其中包含许多文件的备份(“组织”在许多目录和文件的混乱混合中)。有些文件是唯一的,但大多数文件是重复的。我的硬盘上已经有一些文件,但不是全部。

我想从备份中收集所有唯一文件。如果我已经有了这些文件(名称相同,文件类型就是名称,文件大小相同),那么我可以删除备份文件。如果我还没有备份文件,那么我想将它移动到我的硬盘中。最后,我将销毁备份,并且我的硬盘驱动器将拥有每个文件的一个副本,并且没有重复。


这是一个例子:

My Hard Drive contains the following files: [apple, banana, pear]
Backup A contains the following files: [apple, pear, peach, tomato, carrot]
Backup B contains the following files: [apple, banana, pear, peach, tomato, carrot, potato]
Backup C contains the following files: [apple, banana, pear, peach, tomato, carrot, potato, celery]

在上面的例子中,我的程序首先将备份 A 中的桃、番茄和胡萝卜复制到我的硬盘,然后删除备份 A。然后它会从第二个硬盘复制土豆,并删除备份 B。然后它会从备份 C 中复制 celery,并删除备份 C。

【问题讨论】:

    标签: python-3.x directory path os.walk


    【解决方案1】:

    我认为最好的情况是转到 Python 文档并访问:

    1. os module

    2. os.path module

    3. shutil module

    这三个应该足以满足我在您的问题中看到的所有文件操作。但请注意您将编写的代码具有破坏性,因为任何类型的 sn-p 都可能会弄乱您的文件。

    【讨论】:

      猜你喜欢
      • 2013-05-28
      • 2012-12-16
      • 1970-01-01
      • 2012-06-13
      • 1970-01-01
      • 2021-12-08
      • 1970-01-01
      • 2012-06-05
      相关资源
      最近更新 更多