0

So I have written a binary file, and I am attempting to get the checksum of the File. I am not sure whether I am understanding the hashlib library fully, or whether I am understanding exactly how to implement it. Here is what I have, in Python 2.7:

def writefile(self, outputFile): outputFile = open(outputFile, 'wb+') for par in self.fileformat.pList: if par.name.lower() in self.calculated.final.keys(): outputFile.write(self.calculated.final[par.name.lower()]) else: outputFile.write(self.defaults.defaultValues[par.name.upper()]) outputFile.close() with open(outputFile, 'rb') as fh: m = hashlib.md5() while True: data = fh.read(8192) if not data: break m.update(data) print m.digest() outputFile.close() 

what I keep getting is :

TypeError: coercing to Unicode: need string or buffer, file found 

any help would be appreciated, because I could be headed in the completely wrong direction.

2
  • 1
    Can you give us the full traceback? Now we have to guess where the error occurs. Commented Oct 29, 2013 at 17:05
  • 3
    You don't need to do outputFile.close() at the end if you use with. That's what it's for. Commented Oct 29, 2013 at 17:05

1 Answer 1

3

The mistake is in the second call to open:

with open(outputFile, 'rb') as fh: 

Here, outputFile is the file object from the first open call, not the file name. This cannot be used with open, which expects a string (or unicode) argument:

TypeError: coercing to Unicode: need string or buffer, file found

The origin is the first line in the function body, where you overwrite the argument outputFile:

outputFile = open(outputFile, 'wb+') 

To prevent these mistakes:

  • Avoid re-assigning to argument variables
  • Use better names: The argument outputFile is not expected to be a file, but a file name or path. So name it filePath or similar.
Sign up to request clarification or add additional context in comments.

2 Comments

This is correct, although a couple of points remain. It's very bad style to replace parameters in a method body, as it leads to these kinds of problems. Second, it would be more sensible to both write the file and compute it's digest in one pass.
Ah thank you I figured it out, that was silly of me. I was too caught up in getting the hashlib to work I dont know what I was thinking

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.