Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1743460
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Python/Ruby

2016-01-03 17:48:27

1. 这个脚本很短,但是有几个tricky的地方,我目录下有管道一个,还有名字中带有空格文件和名字中带有‘的文件,在把他们加入到列表的时候没有问题,但是使用subprocess.popen()就会报错。所以我对cmd进行了处理。 同时使用stat.S_ISFIFO(os.stat(i).st_mode) 来剔除了pipe。

点击(此处)折叠或打开

  1. import os
  2. import subprocess
  3. import stat

  4. def print_all_md5(dirname):
  5.     ll=[]
  6.     for root,dirs,files in os.walk(dirname):
  7.         for name in files:
  8.             ll.append((os.path.join(root,name)))
  9.     
  10.     for i in ll:
  11.         if stat.S_ISFIFO(os.stat(i).st_mode):
  12.             ll.remove(i)


  13.     for filename in ll:
  14.         if filename.endswith('txt'):
  15.             if "'" in filename:
  16.                 filename=filename.replace("'","\\'")
  17.             if " " in filename:
  18.                 filename=filename.replace(" ","\ ")
  19.             cmd="md5sum {0}".format(filename)
  20.             fp=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
  21.             (s,e)=fp.communicate()
  22.             result=(s.decode()).strip()
  23.             e=(e.decode()).strip()
  24.             print(result,e)

  25. print_all_md5(dirname=".")

稍微作一下修改,可以找到目录下所有md5digest一样的文件。

点击(此处)折叠或打开

  1. import os
  2. import subprocess
  3. import stat

  4. def print_all_md5(dirname,suffix):
  5.     ll=[]
  6.     d={}
  7.     for root,dirs,files in os.walk(dirname):
  8.         for name in files:
  9.             """
  10.             if "'" in name:
  11.                 name=name.replace("'","\'")
  12.                 #print(name)
  13.             if " " in name:
  14.                 name=name.replace(" ","\ ")
  15.             #if name.endswith('txt'):
  16.                 #print(name)
  17.             """
  18.             ll.append((os.path.join(root,name)))
  19.     
  20.     for i in ll:
  21.         if stat.S_ISFIFO(os.stat(i).st_mode):
  22.             ll.remove(i)


  23.     for filename in ll:
  24.         if filename.endswith(suffix):
  25.             if "'" in filename:
  26.                 filename=filename.replace("'","\\'")
  27.             if " " in filename:
  28.                 filename=filename.replace(" ","\ ")
  29.             cmd="md5sum {0}".format(filename)
  30.             fp=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
  31.             (s,e)=fp.communicate()
  32.             result=(s.decode()).strip()
  33.             e=(e.decode()).strip()
  34.             #print(result,e)
  35.             checksum,*nouse=result.split()
  36.             if checksum in d:
  37.                 d[checksum].append(filename)
  38.             else:
  39.                 d[checksum]=[filename]
  40.     return d


  41. def print_duplicates(d):
  42.     for key, names in d.items():
  43.         if len(names) >1:
  44.             print('The following files have the same checksum')
  45.             for name in names:
  46.                 print(name)


  47. def find_dup_file():
  48.     d=print_all_md5(dirname=".",suffix=".txt")
  49.     print_duplicates(d)
  50. find_dup_file()


2. 回头我准备用pathlib重写下path 那一段,pathlib 提供了更多查询文件类型的功能。

点击(此处)折叠或打开

  1. import pathlib
  2. def my_walk(dirname):
  3.     """I will use pathlib to rewrite this function"""
  4.     if '__pycache__' in dirname:
  5.         return name

  6.     p=Path(dirname)
  7.     
  8.     ll=[i for i in p.glob('**/*') if not i.is_fifo() and not i.is_socket() ]

  9.     return ll
3. 感谢下另外一个网友的热心,使用hashlib比我的invoke md5sum的代码要好。

点击(此处)折叠或打开

  1. def print_md5(dirname):
  2.     files = glob.glob("*.txt")
  3.     for f in files:

  4.         print ("=================")
  5.         md5file=open(f,'br')
  6.         md5=hashlib.md5(md5file.read()).hexdigest()
  7.         md5file.close()
  8.         print (md5,f)

  9. print_md5(dirname=".")

阅读(1628) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~