Specific question about readlines versus xreadlines
Pernell Williams
pernell.h.williams.nospam at intel.com
Wed Mar 17 19:21:44 EST 2004
Hi all:
Thank you for your responses. I have a more specific question about
"file.seek() and file.readline()" versus "file.seek() and file.xreadlines".
When I have the following code:
#!/usr/bin/env python
#opening the file for reading
str = 'myShortFile'
file = open(str, 'r')
counter = 0
#outer loop using file.readline
while 1:
line = file.readline()
if line == '':
break
data = line
print data
file.seek(12)
# end while
And the input file is:
abc
def
ghi
The above code with the readline statement is:
abc
In other words, the code reads the first line of the file, goes to position
12 (which is the end of the last line of the file), and then during the
second iteration of the while loop, the position of the file is at 12.
Now, with xreadlines(), it is a different story. I have this code:
#!/usr/bin/env python
#opening the file for reading
str = 'myShortFile'
file = open(str, 'r')
counter = 0
#outer loop using file.xreadlines
for line in file.xreadlines():
data = line
print data
file.seek(12)
# end while
and if I have the same input file as above, I get this output:
abc
def
ghi
In other words, even though I have the "file.seek(12)" statement in the
loop, the position is NOT changed to 12 during the 2nd iteration of the
while loop.
Is there ANYTHING I can do in this "xreadlines()" loop to make it go to
position 12 in the second iteration?
Thanks for your help!
-Pernell
--
Remove ".nospam" from e-mail address to reply
More information about the Python-list
mailing list