Reputation: 394
I am testing one of the scenarios for my nosql-database setup where I am storing blobs of PDF data. Later, want to use if it can retrieve the original pdf document. Just to test it, I wrote a small test code in Python.
import base64
with open('test.pdf', 'rb') as f:
blob = base64.b64encode(f.read())
text_file = open('test_blob.txt', "w")
text_file.write(blob)
text_file.close()
with open('test_blob.txt', 'r') as f:
blob=f.read().decode('base64')
text_file = open('result.pdf', "w")
text_file.write(blob)
text_file.close()
When I checked to see the result.pdf, it was corrupt. What could be the problem?
Upvotes: 2
Views: 9027
Reputation: 91
I tried the same code that you have provided, but unfortunately I'm getting error in decode part. I have modified the code a bit. Please try this and let me know if it works.
import base64
with open('path/sample.pdf', 'rb') as f:
blob = base64.b64encode(f.read())
text_file = open('test_blob.txt', "wb")
text_file.write(blob)
text_file.close()
with open('test_blob.txt', 'r') as f:
blob=f.read()
blob = base64.b64decode(blob)
text_file = open('result.pdf','wb')
text_file.write(blob)
text_file.close()
Upvotes: 6