read text file byte by byte

daved170 daved170 at gmail.com
Tue Dec 15 03:25:05 EST 2009


On 13 דצמבר, 22:39, Dennis Lee Bieber <wlfr... at ix.netcom.com> wrote:
> On Sat, 12 Dec 2009 22:15:50 -0800 (PST), daved170 <daved... at gmail.com>
> declaimed the following in gmane.comp.python.general:
>
> > Thank you all.
> > Dennis I really liked you solution for the issue but I have two
> > question about it:
> > 1) My origin file is Text file and not binary
>
>         Do you need to process the bytes in the file as they are? Or do you
> accept changes in line-endings (M$ Windows "text" files use <cr><lf> as
> line ending, but if you read it in Python as "text" <cr><lf> is
> converted to a single <lf>.
>
> > 2) I need to read each time 1 byte. I didn't see that on your example
> > code.
>
>         You've never explained why you need to READ 1 byte at a time, vs
> reading a block (I chose 1KB) and processing each byte IN THE BLOCK.
> After all, if you do use 1 byte I/O, your program is going to be very
> slow, as each read is blocking (suspends) while asking the O/S for the
> next character in the file (this depends upon the underlying I/O library
> implementation -- I suspect any modern I/O system is still reading some
> block size [256 to 4K] and then returning parts of that block as
> needed). OTOH, reading a block at a time makes for one suspension and
> then a lot of data to be processed however you want.
>
>         You originally stated that you want to "scramble" the bytes -- if
> you mean to implement some sort of encryption algorithm you should know
> that most of them work in blocks as the "key" is longer than one byte.
>
>         My sample reads in chunks, then the scramble function XORs each byte
> with the corresponding byte in the supplied key string, finally
> rejoining all the now individual bytes into a single chunk for
> subsequent output.
> --
>         Wulfraed         Dennis Lee Bieber               KD6MOG
>         wlfr... at ix.netcom.com      HTTP://wlfraed.home.netcom.com/

Hi All,
As I read again your comments and the codes you posted I realize that
I was mistaken.
I don't need to read the file byte by byte. you all right. I do need
to scramble each byte. So I'll do as you said - I'll read blocks and
scramble each byte in the block.
And now for my last question in this subject.
Lets say that my file contains the following line: "Hello World".
I read it using the read(1024) as you suggested in your sample.
Now, how can I XOR it with 0xFF for example?
Thanks again
Dave



More information about the Python-list mailing list