Your goal is to be able to create a python script like example.py that will:
1. Read input.bin byte by byte
2. Convert the value of each byte in input.bin into a string
3. Save the output into a file output.txt
For example, given an input.bin that contains this format:
[data ( bytes)]
The resulting output.txt will contain
Generally, to convert binary data to a string you have to use base64 encoding
str_base64 = base64.encodestring(open('input.bin',"rb").read())
# test it
# should only contain printable characters
# 0-9, A-Z, a-z, /+ and a few others (also \n)
# now save str_base64 to a text file
# to convert it back to binary use
fin = open ('input.bin')
fout = open ('output.bin')
x = fin.readlines (1)
open( file_name ) defaults to read mode. You want open( file_name, 'w' ) for your output file so that you may write to it.
This first part is moving the file cursor. Are you sure you want to do this?
The first fin.readline() does not store the line anywhere.
The x = fin.readlines(1) already reads in a string, so saying str(x) isn't doing anything... (especially since you're not storing the result of str(x) ).
for row in fin:
print open ("input.bin", "r")
What? You are iterating over each line of your input file here. But on each iteration you are reopening "input.bin" for reading?
for line in open ("input.bin", "r"):
fout = open ("output.txt", "w")
Now you are opening your input file once more, not storing the file handler but still iterating over it. On each iteration you're trying to print output.txt .... this should be throwing an error since you never created any thing called output, especially an output with an attribute txt.
Then at the end you're simply opening the file "output.txt" for writing and not writing anything to it.
I don't believe that your program will even run.
File reading and writing should be done something like this:
fh = open('input.bin', 'r')
fo = open('output.txt', 'w')
for line in fh:
# do something to line here, storing to another variable like new_line
fo.write( new_line ) # This is how you write a line to the file that is open for writing