[Tutor] Readlines

Jeff Shannon jeff@ccvcorp.com
Mon Jun 23 16:33:02 2003


Adam Vardy wrote:

>Now I'm trying Read(). I have a couple observations, and wonder what
>you folks think. Take the code below. I notice that Read() is
>apparently incredibly slow! Text appears on screen, cursor slowly
>gliding across. Which surprises as it is only reading a byte at a
>time. On a Gigahertz machine.
>

Well, for starters, if this is running at all, you've left out at least 
a couple of lines.  (You'd need to initialize c and z to some value in 
order to avoid getting a NameError.)  

As for why it's not working right... you never specify just *what* it's 
doing wrong, but I can see one problem.  Think a bit about the exit 
conditions for your inner loop --

>while c!='':
>    while z!='\n':
>        z=fhand.read(1)
>        print z,
>    ln=ln+1
>    c=z
>    if ln%100==0:
>        print ln
>        time.sleep(1)
>

The inner loop runs until z is '\n', and then it moves the '\n' into c. 
 Because z cannot be anything except '\n' at the end of the inner loop, 
that means that c will always be '\n', and you could just as well 
replace the line c=z with c='\n'.  At this point, you may see where the 
problem is -- since c is always a newline, it will never be an empty 
string '', and your outer loop will therefore never finish.  Also, as an 
artifact of the way that 'print' works, a space character is printed in 
between each character read from the file -- 'print' assumes that each 
variable given to it is a separate word (or larger unit), and tries to 
do a minimal amount of formatting based on that assumption.  If you want 
to specify every byte that's printed, you need to use sys.stdout.write() 
instead.

Note that 'z = fhand.readline()' will accomplish about the same thing 
that your inner loop is trying to do, and will leave z containing an 
entire line instead of just a newline character.  This would allow you 
to keep your outer-loop structure more or less the same, if you really 
want that, since after the final newline z (and therefore c) will be an 
empty string.  However, if you just want to read each line and print it, 
with a pause every 100 lines, this would be a lot simpler:

fhand = file(filepath,'r')

ln = 0
for line in fhand.xreadlines():
    print line
    ln += 1
    if ln % 100 == 0:
        print ln
        time.sleep(1)

Jeff Shannon
Technician/Programmer
Credit International