Skip to main content
 首页 » 编程设计

python之使用python代码从目录和子目录中删除重复文件

2024年10月24日11yyy_WW

我正在尝试遍历目录和子目录以查找重复文件,但这里遇到的问题是脚本给出了一些错误:

Traceback (most recent call last): 
  File "./fileDupchknew.py", line 29, in <module> 
    dup_fileremove(dirname) 
  File "./fileDupchknew.py", line 26, in dup_fileremove 
    os.remove(filepath) 
  OSError: [Errno 21] Is a directory: '/tmp/rishabh-test/new-test' 

脚本:

#!/usr/bin/python 
import os 
import hashlib 
import sys 
 
 
dirname = sys.argv[1] os.chdir(dirname) 
 
 def dup_fileremove(dir): 
    duplicate = set() 
    os.chdir(dir) 
    path=os.getcwd() 
    print ("The dir is: ", path) 
    for filename in os.listdir(dir): 
        filehash = None 
        filepath=os.path.join(dir, filename) 
        print("Current file path is: ", filepath) 
        if os.path.isdir(filepath): 
            dup_fileremove(filepath) 
        elif os.path.isfile(filepath): 
            filehash =hashlib.md5(file(filepath).read()).hexdigest() 
        if filehash not in duplicate: 
            duplicate.add(filehash) 
        else: 
            os.remove(filepath) 
            print("removed : ", filepath) 
 
dup_fileremove(dirname) 

请您参考如下方法:

因为你不想删除目录(从有问题的评论中可以看出)-

No i don't want to delete directories

如果是上述情况,则会出现您的问题,因为您没有为目录创建文件哈希。因为当您不为目录创建文件哈希时,您得到的文件哈希为 None ,而对于第一个目录, None 不存在于 duplicates set ,因此它将 None 添加到集合中。从下一个目录开始,它发现 None 已经存在于 set() 中,因此它尝试使用 os.remove()它导致了问题。

一个简单的修复方法是在尝试删除之前以及添加到集合之前检查 filehash 是否为 None 。示例 -

#!/usr/bin/python 
import os  
import hashlib 
import sys 
 
 
dirname = sys.argv[1]  
os.chdir(dirname) 
 
 def dup_fileremove(dir): 
    duplicate = set() 
    os.chdir(dir) 
    path=os.getcwd() 
    print ("The dir is: ", path) 
    for filename in os.listdir(dir): 
        filehash = None 
        filepath=os.path.join(dir, filename) 
        print("Current file path is: ", filepath) 
        if os.path.isdir(filepath): 
            dup_fileremove(filepath) 
        elif os.path.isfile(filepath): 
            filehash =hashlib.md5(file(filepath).read()).hexdigest() 
        if filehash is not None and filehash not in duplicate: 
            duplicate.add(filehash) 
        elif filehash is not None: 
            os.remove(filepath) 
            print("removed : ", filepath) 
 
dup_fileremove(dirname)