file = open(path,'wb')?????

Max M maxm at mxm.dk
Tue Sep 24 04:53:04 EDT 2002


jubafre at brturbo.com wrote:


> i open the a file for writing in a binary mode, but doesnt work
> 
> final3=['0020 0000x9', '0030 0000xa', '00B4 0000x8', '0030 0000xb', '00F0', '0001', '0003', '0005']
> destino=raw_input("Digite o nome do arquivo .mem: ")
> f = open(destino,'wb')
> f.write('\x03AHM ')
> for i in final3:
>     f.write(i+' ')
> f.close()
> 
> 
>  i open the file in the notepad and my file appears like a text file and not as a binary file, i dont know why??
> 
> 
>>>file.mem
>>
> AHM 0020 0000x7 0030 0000x8 0010 0000x9 00F0 0003 0002 0000 
> 
> 
>>>i want the file like this, how??
>>
> AHM  € 0 ?  ƒ ð 


You are indeed writing strings to the file. Setting a file to binary 
mode only affects a few newline characters on Windows. Nothing else.

You should use the struct module for writing binary values.

4.3 struct -- Interpret strings as packed binary data

regards Max M




More information about the Python-list mailing list