Expected Output
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

thanks...

Sounds like something my lecturer at University would ask us to do...
Have you at least made an attempt at it? If so, want to provide us with what you already have?

# Read input.bin
#   - Format: [size (1 byte)] [data ( [size] bytes)]    
# Output: output.txt containing data from "input.bin"
import os
import struct
import string
import sys
import traceback
#import bin
#import txt

#Function
#input=input.bin.readline()
#input=string.strip(input)
#def fin(open):
#def open(input.bin): 
#fin = open ("input.bin", "r")
fin = open ('input.bin')
fout = open ('output.bin')
fin.readline()
#fin.close()
x = fin.readlines (1)
str (x)

for row in fin:
    print open ("input.bin", "r")

for line in open ("input.bin", "r"):
    print output.txt,

fout = open ("output.txt", "w")
#fout.close()
      #import csv
      #rf = open('sample.txt') #input file handle
      #wf = open('writer.csv','w') #output file handle
      #writer = csv.writer(wf)
      #for row in rf.readlines():
      #writer.writerow(row.split())
      #rf.close() # close input file handle
      #wf.close() # close output file hanle

theres what i did but nothing happens

plz edit my code..thanks...i really need this one

Most of the lines are commented , what exact problem are you facing ?

Generally, to convert binary data to a string you have to use base64 encoding

import base64
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)
print  str_base64 

# now save str_base64 to a text file

# to convert it back to binary use 
# base64.decodestring(str_base64)
fin = open ('input.bin')
fout = open ('output.bin')
fin.readline()

x = fin.readlines (1)
str (x)

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"):
    print output.txt,

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

fh.close()
fo.close()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.