Python:TypeError:需要一个整数

时间:2013-03-18 22:27:52

标签: python md5 hashlib

我试图获取某些文件的md5校验和并将它们写入临时文件。

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 import *”做了这个错误,但是我没有这样做,也没有在任何其他模块上使用import *。

1 个答案:

答案 0 :(得分:1)

open()的第三个参数应该是一个整数:

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

可选的缓冲参数指定文件所需的缓冲区大小:0表示无缓冲,1表示行缓冲,任何其他正值表示使用(大约)该大小(以字节为单位)的缓冲区。负缓冲意味着使用系统默认值,通常为tty设备进行行缓冲,并为其他文件进行完全缓冲。如果省略,则使用系统默认值。 [2]