Read binary file and dump data in

Santiago Romero sromero at gmail.com
Tue Jan 13 15:02:54 EST 2009


 Hi.

 Until now, all my python programs worked with text files. But now I'm
porting an small old C program I wrote lot of years ago to python and
I'm having problems with datatypes (I think).

 some C code:

 fp = fopen( file, "rb");
 while !feof(fp)
 {
    value = fgetc(fp);
    printf("%d", value );
 }

 I started writing:

 fp = open(file, "rb")
 data = fp.read()
 for i in data:
   print "%d, " % (int(i))

 But it complains about i not being an integer... . len(data) shows
exactly the file size, so maybe is a "type cast" problem... :-?

 What's the right way to work with the binary data (read 1 byte values
and work with them, dumping them as an integer in this case)?

 Thanks.



More information about the Python-list mailing list