2013-03-18 67 views
0

我想获取某些文件的md5校验和并将它们写入临时文件。Python:TypeError:需要一个整数

import os 
import hashlib 

PID = str(os.getpid()) 
manifest = open('/temp/tmp/MANIFEST.'+ PID + '.tmp','w') #e.g. MANIFEST.48938.tmp 
for elmt in files_input: 
    input = open(elmt['file'], "r", 'us-ascii') #'us-ascii' when I ran "file --mime" 
    manifest.write(hashlib.md5(input.read()).hexdigest()) 

从此,我感到我还没有能够解决一个Python错误:

Traceback (most recent call last): 
File "etpatch.py", line 131, in <module> 
    input = open(elmt['file'], "r", 'us-ascii') 
TypeError: an integer is required 

一些人不得不从这样的错误“从OS进口*”,但我不这样做我也不会在任何其他模块上使用导入*。

+0

你从哪里得到来自'美国ascii'参数的想法?第三个参数应该为* nix shell提供一个缓冲区大小(或0表示无缓冲,或1表示缓冲行) – 2013-03-18 22:30:57

+0

:文件--mime-encoding imagineerThat 2013-03-18 22:32:18

+1

不,我的意思是你为什么要把它作为第三个参数打开? – 2013-03-18 22:33:06

回答

1

的第三个参数open()预期为一个整数:

open(name[, mode[, buffering]]) 

The optional buffering argument specifies the file’s desired buffer size: 0 means unbuffered, 1 means line buffered, any other positive value means use a buffer of (approximately) that size (in bytes). A negative buffering means to use the system default, which is usually line buffered for tty devices and fully buffered for other files. If omitted, the system default is used. [2]

+0

谢谢。如果我拿出违规的参数,我会在下一行使用hashlib时得到“TypeError:Unicode对象必须在散列之前进行编码”。我试着做“打开(elmt ['file'],”r“,encoding ='us-ascii')”但我得到了同样的错误。 – imagineerThat 2013-03-18 22:39:13

相关问题