Thursday, December 29, 2016

TypeError: write() argument must be str, not bytes

Recently I encountered this error: "TypeError: write() argument must be str, not bytes" when trying to write a PDF file from anaconda 3. The solution was to write stream in binary.


Error:

J:\>python pdf.py
Traceback (most recent call last):
  File "pdf.py", line 31, in <module>
    fd.write(buf.getvalue())
TypeError: write() argument must be str, not bytes

Problem code:

# Write the PDF to a file
with open('test.pdf', 'w') as fd:
    fd.write(buf.getvalue())

Solution:

# Write the PDF to a file
with open('test.pdf', 'wb') as fd:
    fd.write(buf.getvalue())

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.