read text file byte by byte

Rhodri James rhodri at wildebst.demon.co.uk
Sat Dec 12 17:40:36 EST 2009


On Sat, 12 Dec 2009 11:14:13 -0000, census <census at no-email.de> wrote:

> Steven D'Aprano wrote:
>
>> On Sat, 12 Dec 2009 10:35:55 +0100, census wrote:
>>
>>>> I've got some questions -
>>>> 1) How do I read the file byte by byte 2) Should I use streams? If so
>>>> and I get my entire scrambled text in stream can I just write it to  
>>>> the
>>>> binary file?
>>>>
>>>> Thanks
>>>> Dave
>>>
>>> f = open ("binaryfile", "r")
>>> bytearray = map (ord, f.read () )
>>>
>>> Stores the content of binaryfile in the list bytearray.
>>
>> If it's a binary file, you should open it in binary mode:
>>
>> f = open ("binaryfile", "rb")
>>
>>
>>
>
> Add the "b" flag to both in and out file if you prefer it:

It's not a matter of preferring.  Depending on your operating system  
and/or version of Python, reading a binary file without the "b" flag will  
give you wrong answers.

-- 
Rhodri James *-* Wildebeest Herder to the Masses



More information about the Python-list mailing list