2013-03-18 64 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

相關問題