免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4575 | 回复: 7
打印 上一主题 下一主题

python如何获取返回值的一部分 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-06-24 17:19 |只看该作者 |倒序浏览
要对文件进行md5值的计算,DOS下md5.exe工具,代码如下:
  1. import os,sys
  2. import subprocess

  3. sAPP = '"e:\md5.exe"'
  4. sFile = 'D:\\python_sh\\test1.log'

  5. m = subprocess.Popen(sAPP + " " + sFile)
  6. print m
复制代码
结果返回如下:
<subprocess.Popen object at 0x00BBBC90>
8B1A9953C4611296A827ABF8C47804D7  D:\python_sh\test1.log

怎么样可以仅取得MD5值,其他的信息都不要?

工具地址:
http://bbs.chinaunix.net/thread-2181225-1-1.html

论坛徽章:
11
技术图书徽章
日期:2014-03-01 14:44:34天蝎座
日期:2014-05-21 22:11:59金牛座
日期:2014-05-30 17:06:14
2 [报告]
发表于 2013-06-24 17:48 |只看该作者
不用这么复杂吧,用标准库的hashlib.md5

论坛徽章:
1
2015七夕节徽章
日期:2015-08-21 17:58:43
3 [报告]
发表于 2013-06-24 17:51 |只看该作者
没有windows下的python环境,lz可以尝试下:
print m.read().split('\r\n')[1].split()[0]

论坛徽章:
0
4 [报告]
发表于 2013-06-25 09:35 |只看该作者
回复 2# timespace


    标准的md5库对文件大小有限制,超过1G的文件比较内存报错。

论坛徽章:
0
5 [报告]
发表于 2013-06-25 09:36 |只看该作者
shreychen 发表于 2013-06-24 17:51
没有windows下的python环境,lz可以尝试下:
print m.read().split('\r\n')[1].split()[0]


报下面的错误:

Traceback (most recent call last):
  File "C:\Documents and Settings\Administrator\����\auto test\hello_test\ttttttt.py", line 45, in <module>
    print m.read().split('\r\n')[1].split()[0]
AttributeError: 'Popen' object has no attribute 'read'
8B1A9953C4611296A827ABF8C47804D7  D:\python_sh\test2.log

论坛徽章:
11
技术图书徽章
日期:2014-03-01 14:44:34天蝎座
日期:2014-05-21 22:11:59金牛座
日期:2014-05-30 17:06:14
6 [报告]
发表于 2013-06-25 14:00 |只看该作者
回复 4# 月中井
你是否把整个文件读入内存了?看我的demo(md5check.py),增量计算md5
  1. #!/usr/bin/env python

  2. import hashlib
  3. import sys

  4. def main():
  5.     if len(sys.argv) != 2:
  6.         sys.exit('Usage: %s file' % sys.argv[0])

  7.     filename = sys.argv[1]
  8.     m = hashlib.md5()
  9.     with open(filename, 'rb') as fp:
  10.         while True:
  11.             blk = fp.read(4096) # 4KB per block
  12.             if not blk: break
  13.             m.update(blk)
  14.     print m.hexdigest(), filename

  15. if __name__ == '__main__':
  16.     main()
复制代码
例子,计算一个3.5GB的视频文件md5
  1. is-iMac:Wreck-It.Ralph.2012.BluRay.720p.DTS.x264-CHD Guorui$ ll -h
  2. total 7352528
  3. drwxrwxrwx  1 Guorui  staff   264B  6 25 13:51 ./
  4. drwxrwxrwx  1 Guorui  staff   4.9K  6 22 19:21 ../
  5. -rwxrwxrwx  1 Guorui  staff   147K  2 18 22:42 Wreck-It.Ralph.2012.BluRay.720p.DTS.x264-CHD.chs&eng.srt*
  6. -rwxrwxrwx  1 Guorui  staff   3.5G  2 22 22:27 Wreck-It.Ralph.2012.BluRay.720p.DTS.x264-CHD.mkv*
  7. -rwxrwxrwx  1 Guorui  staff   365B  6 25 13:50 md5check.py*
  8. Lis-iMac:Wreck-It.Ralph.2012.BluRay.720p.DTS.x264-CHD Guorui$ ./md5check.py Wreck-It.Ralph.2012.BluRay.720p.DTS.x264-CHD.mkv
  9. 73a8785c5d84af640bfa44810842e5a5 Wreck-It.Ralph.2012.BluRay.720p.DTS.x264-CHD.mkv
  10. Lis-iMac:Wreck-It.Ralph.2012.BluRay.720p.DTS.x264-CHD Guorui$ ./md5check.py
  11. Usage: ./md5check.py file
  12. Lis-iMac:Wreck-It.Ralph.2012.BluRay.720p.DTS.x264-CHD Guorui$
复制代码

论坛徽章:
0
7 [报告]
发表于 2013-06-25 14:29 |只看该作者
回复 6# timespace


    是的,之前不知道怎么做增量的比较。谢谢你的回复,解决了我的问题。

论坛徽章:
0
8 [报告]
发表于 2013-06-25 15:07 |只看该作者
本帖最后由 月中井 于 2013-06-25 15:10 编辑
月中井 发表于 2013-06-25 09:36
Traceback (most recent call last):
  File "C:\Documents and Settings\Administrator\����\auto test\hello_test\ttttttt.py", line 45, in <module>
    print m.read().split('\r\n')[1].split()[0]
AttributeError: 'Popen' object has no attribute 'read'


使用第三方的md5工具,做如下修改后,问题解决:

import os,sys
import subprocess
#from subprocess import Popen, PIPE, STDOUT

sAPP = 'e:\md5.exe'
sFile = 'D:\\python_sh\\test2.log'

m = subprocess.Popen(sAPP + " " + sFile,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
m_stdout = m.stdout.readlines()
print m_stdout[0].split()[0]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP