1

I have a Python (v2.7) script that uses the Bitbucket-api package to create an archive of a project boilerplate in Bitbucket and extract that to create a new project from that boilerplate code.

Almost everything is working great, but any images in that boilerplate are corrupted when extracted to create the new project's code base. I'm new to Python, so I'm hoping someone can give me a hand with this. I believe that the corruption is getting introduced in this line, but that's just a semi-educated guess.

If I print the file content before writing it, it looks something like this:

�PNG

IHDRo��yIDAT(�c8s�Q0
�
 p�M\�KIEND�B`�

So...binary. The current .write(content.encode('utf-8')) doesn't seem to work for binary content.

What do I need to do to write that content to a file without corrupting it?

1
  • 1
    Just to check, are you using 'wb' mode for your file handler to write your files? Commented Mar 19, 2015 at 11:45

1 Answer 1

1

Your error lies in a different spot. You act as if every HTTP response is text, which is obviously not true.

You need to get the binary content, and never encode/decode unless you know it's text and you need to do text processing.

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

1 Comment

Ah, thanks, @orlp. I'll focus some energy in that area, but I see exactly what you're saying.

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.