-2

my friend mailed me a binary file "Masters.dat" coded in Python.I want to read the codes inside the binary file,so how do I do it?

I have tried :-

file = open("C:\Users\Samanyou\Desktop\Source_XII\Project\Masters.dat", "rb")

read=file.readlines()

print read

But this gives me the result in ASCII or something else but not in human readable form.

1
  • Find the Python source that created the file in the first place. That should give you an indication of how to read it back.
    – cdarke
    Dec 20, 2014 at 9:48

1 Answer 1

1

readlines is meant to work with text files, not binary ones. For binary files you'd typically use read to get chunk of bytes -- but there's no way to make such chunks "human readable" unless you know what detailed format was used to write the file (in which case you can use e.g struct to decode it back into Python data and format them as you wish). So your friend had better send you info about exactly how the file was written in the first place!-)

2
  • Can you please provide me the exact code to convert the file into text format so that I can read it?Just provide me the code so that I can read it in text form.Its a school project of mine. Dec 20, 2014 at 6:14
  • @SamanyouGarg: We don't know how your friend encoded that file, and we have no idea of the file's contents, so how do you expect us to know how to decode it???
    – PM 2Ring
    Dec 20, 2014 at 6:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.