Write decoded from base64 string to file

15,432

Solution 1

You probably figured out of to do that now 5 years later, but here is the solution if anyone needs it.

import base64

with open('Input.txt', 'r') as input_file:
  coded_string = input_file.read()
decoded = base64.b64decode(coded_string)
with open('Output.txt', 'w', encoding="utf-8") as output_file:
  output_file.write(decoded.decode("utf-8"))

Solution 2

under windows you open with 'rb' instead of 'r'.

in your case your code should be :

input_file = open('Input.txt', 'rb')

instead of

input_file = open('Input.txt', 'r')

btw: http://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files

On Windows, 'b' appended to the mode opens the file in binary mode, so there are also modes like 'rb', 'wb', and 'r+b'. Python on Windows makes a distinction between text and binary files; the end-of-line characters in text files are automatically altered slightly when data is read or written. This behind-the-scenes modification to file data is fine for ASCII text files, but it’ll corrupt binary data like that in JPEG or EXE files. Be very careful to use binary mode when reading and writing such files. On Unix, it doesn’t hurt to append a 'b' to the mode, so you can use it platform-independently for all binary files.

hope it helps

Share:
15,432
olyv
Author by

olyv

Software Testing Engineer

Updated on September 15, 2022

Comments

  • olyv
    olyv over 1 year

    the question is that how to write string decoded from base64 to a file? I use next piece of code:

    import base64
    
    input_file = open('Input.txt', 'r')
    coded_string = input_file.read()
    decoded = base64.b64decode(coded_string)
    output_file = open('Output.txt', 'w')
    output_file.write(decoded)
    output_file.close()
    

    Input.txt contains base64 string (smth. like PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz48cmV2aW). After script execution I suppose to see xml in Output.txt but output file contains some wrong symbols (like <?xml version="1.0" encoding="UTF-8"?><review-case create®vFFSТ#2). At the same time if I not read from base64 string from file Input.txt but specify it in script as coded_string = '''PD94bWwgdmVyc2lvbj0iMS4wIiBlbm...''' then Output.txt contains correct xml. Is this something wrong with utf encoding? How to fix this? I use Python2.7 on Windows 7. Thanks in advance.