0

I'm using a platform that, when you upload pdf's to it, converts the pdf with the base64 encode in Python. Then it stores the binary string in the database.

Now I want to decode the strings and write them to a local folder, so I thought to use the 'with open' structure and pass it the b parameter for binary and then it should create test.pdf based of my decoded string and write it to my desktop? Yet this yields no results, what am I doing wrong here?

code = "My binary string"

with open('test.pdf', 'wb') as fout:
     fout.write(base64.decode(code, '~/Desktop'))

EDIT:

code = "My binary string"

with open('~/Desktop/test.pdf', 'wb') as fout:
     fout.write(base64.decodestring(code))

Example binary string in database: "65/658e9014babd33786821f3130c5f3a1cc1322ddf" So I'm assuming it starts after the '/' mark?

RandomPerson
  • 790
  • 2
  • 8
  • 29

1 Answers1

10

base64.decode(,) takes files as args. you want to try

fout.write(base64.decodestring(code))

though your code example is not encoded. Here's a working example:

#!/usr/bin/python
import base64, os
code = 'TXkgYmluYXJ5IHN0cmluZw==\n'
with open(os.path.expanduser('~/Desktop/test.pdf'), 'wb') as fout:
     fout.write(base64.decodestring(code))
meuh
  • 11,500
  • 2
  • 29
  • 45
  • It doesn't seem to create test.pdf with this method? – RandomPerson Jul 15 '15 at 11:53
  • @RandomPerson If you are really using `~` in your filename you will need to call `os.path.expanduser()` to have it converted into a real path. I've edited my answer with a working example. – meuh Jul 15 '15 at 12:00
  • Thanks! If I do so I get an unrecognizable pdf file. I've added an example of the string that is being saved in the database – RandomPerson Jul 15 '15 at 12:09
  • Perhaps the dbase format needs some further unpacking before being decoded? All pdf's start with the string `%PDF`. If you encode that it gives `JVBER...` which is nothing like what you have. – meuh Jul 15 '15 at 12:23
  • 1
    it appears decodestring() no longer works if you are using python 3.9. Instead you can use code.encode() then use decodebytes(). Works like a charm. – Singularity20XX Feb 21 '22 at 21:25