0

I am trying to get the md5 checksum of some files and write them into a temp file.

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()) 

From this I get a Python error that I haven't able to resolve:

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

Some people have had this error from doing "from os import *" but I am not doing this nor am I using import * on any other module.

4
  • Where did you get the idea for the us-ascii argument from? That third argument is supposed to give a buffer size (or 0 for unbuffered, or 1 for line buffered) Commented Mar 18, 2013 at 22:30
  • on a *nix shell: file --mime-encoding <filename> Commented Mar 18, 2013 at 22:32
  • 1
    No, I mean why did you give that as the third argument to open? Commented Mar 18, 2013 at 22:33
  • Took it out. See my comment under NPE's answer. Commented Mar 18, 2013 at 22:39

1 Answer 1

1

The third argument to open() is expected to be an integer:

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]

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. If I take out the offending argument, I get "TypeError: Unicode-objects must be encoded before hashing" in the next line I use hashlib. I tried doing "open(elmt['file'], "r", encoding='us-ascii')" but I get the same error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.