- 论坛徽章:
- 6
|
回复 3# zhonghua7896321
我刚才发现我的解决办法有问题,对文件名含有引号的文件没法获得md5值。
另外一个解决办法:引入hashlib模块来解决
[root@server01 Test]# python find_file.py
a .txt
(u'd41d8cd98f00b204e9800998ecf8427e a\nd41d8cd98f00b204e9800998ecf8427e .txt', u'')
=================
d41d8cd98f00b204e9800998ecf8427e
1233'atdz.txt
(u'', u"/bin/sh: -c: line 0: unexpected EOF while looking for matching `''\n/bin/sh: -c: line 1: syntax error: unexpected end of file")
=================
68b329da9893e34099c7d8ad5cb9c940
A.txt
(u'333acdcdb62bd6d001b275c885f807f5 A.txt', u'')
=================
333acdcdb62bd6d001b275c885f807f5
[root@server01 Test]# ll
total 16
-rw-r--r-- 1 root root 1 Jan 4 21:51 1233'atdz.txt
-rw-r--r-- 1 root root 0 Jan 4 21:40 a
-rw-r--r-- 1 root root 0 Jan 4 21:45 a .txt
-rw-r--r-- 1 root root 68 Jan 4 21:39 A.txt
-rw-r--r-- 1 root root 588 Jan 4 21:54 find_file.py
-rw-r--r-- 1 root root 210 Jan 4 21:39 remove.py
-rw-r--r-- 1 root root 0 Jan 4 21:40 x.html
[root@server01 Test]#
[root@server01 Test]# cat find_file.py
import glob
import stat
import subprocess
import hashlib
def print_md5(dirname):
files = glob.glob("*.txt")
for f in files:
print f
cmd = "md5sum " + f
fp = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(s,e) = fp.communicate()
result = (s.decode()).strip()
e = (e.decode()).strip()
print (result, e)
print "================="
md5file=open(f)
md5=hashlib.md5(md5file.read()).hexdigest()
md5file.close()
print md5, "\n\n"
print_md5(dirname=".")
PS: 二楼的办法也不错,这样操作可以直接引用楼主原来的代码 |
|