Re: read text file byte by byte

2009-12-12 Thread census
daved170 wrote:

> Hello everybody,
> I need to read a text file byte after byte.
> Eache byte is sent to a function that scramble it
> and I need to write the result to binary file.
>
> 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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: read text file byte by byte

2009-12-12 Thread census
daved170 wrote:

> Hello everybody,
> I need to read a text file byte after byte.
> Eache byte is sent to a function that scramble it
> and I need to write the result to binary file.
>
> 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

OK, now here a complete code to read a file byte by byte, scramble each
byte (with a really complex algorithm in my example) and write the
output to another file.

def scramble (a): return (a + 13) % 256

infile = open ("binaryfile1", "r")
outfile = open ("binaryfile2", "w")

bytearray = map (ord, infile.read () )
scrambled = map (scramble, bytearray)
map (lambda x : outfile.write (chr (x) ), scrambled)

infile.close ()
outfile.flush ()
outfile.close ()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: read text file byte by byte

2009-12-12 Thread census
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:

def scramble (a): return (a + 13) % 256

infile = open ("binin", "rb")
outfile = open ("binout", "wb")

bytearray = map (ord, infile.read () )
scrambled = map (scramble, bytearray)
map (lambda x : outfile.write (chr (x) ), scrambled)

infile.close ()
outfile.flush ()
outfile.close ()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Caps Lock State on Windows

2009-12-12 Thread census
N3wCr4Zy wrote:

> how to i get Caps Lock state (on/off) on windows with win32api?

public static extern short GetKeyState(int keyCode)
in user32.dll
-- 
http://mail.python.org/mailman/listinfo/python-list