473,398 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Specific question about readlines versus xreadlines

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
Jul 18 '05 #1
2 3334
Pernell Williams wrote:
Thank you for your responses. I have a more specific question about
"file.seek() and file.readline()" versus "file.seek() and
file.xreadlines".


The latter won't work, since F.xreadlines buffers its reads.

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ Everyone wants to look good at his own funeral.
-- Louis Wu
Jul 18 '05 #2
Erik gave you the correct answer. I just thought I'd point out a few other
things about your code:

In message <c3**********@news01.intel.com>, Pernell Williams wrote:
#opening the file for reading

str = 'myShortFile'
Did you know that str() is a Python function that returns the string
representation of an object. To see how it works, run Python interactively
and pass it some objects:
print str(3)
print str('hello')
d = {"one": 1, "two": 2, "three": 3}
print str(d)
In other words, it's not a good idea to redefine str in your code as it
hides the original function making it unusable. Also...
file = open(str, 'r')
In Python file() is a function that happens to be an alias of open(). So if
you redefine it like this you can't do:
f = file('myFile', 'r')
This is OK if you only intend to use open() but redefining existing Python
names is still arguably not good practice.
while 1:

line = file.readline()

if line == '':

break
The usual way to read lines from a file is like this (assuming you followed
my previous suggestion and stopped using 'file' for the filename:
for line in f.readlines():
# process line
This will loop until line == '' so you don't even need the break statement.
And you can make it shorter still:
for line in f:
# process line
And you can shorten the whole process further still by combining the open
with the read:
for line in open(filename):
# process line

I realise that your example is specifically comparing and contrasting the
readline() function with the xreadlines() function, but I wasn't sure if
you were aware of the different ways of doing the same thing. If you were,
please ignore the above paragraph: hopefully it will prove useful to
newcomers to Python.
data = line

print data


I'm curious as to why you didn't just do 'print line' rather than assigning
it to 'data' first.

--
Garry Knight
ga*********@gmx.net ICQ 126351135
Linux registered user 182025
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Leeds, Mark | last post by:
I did more investigation into my previous problem and what happens is that my text file has \r for representing a new line instead of a \n. is there a way to tell the readlines function that the...
2
by: Yong Wang | last post by:
Hi, I use readlines() to read one data file. Python automatically parses the read contents into a list of lines. When I used list to print out the 1st line, it is ok. When I use the list index 2...
9
by: wordsender | last post by:
Hey guys, I can't figure this one out, why is this simple script giving me problems? logfile=file(r'test.txt','w') logfile.write('datetime') test=logfile.readlines() When I run it I get...
3
by: Eric Brunel | last post by:
Hi all, I just found a problem in the xreadlines method/module when used with codecs.open: the codec specified in the open does not seem to be taken into account by xreadlines which also returns...
2
by: vch | last post by:
Does a call to file.readlines() reads all lines at once in the memory? Are the any reasons, from the performance point of view, to prefer *while* loop with readline() to *for* loop with readlines()?
34
by: Ross Reyes | last post by:
HI - Sorry for maybe a too simple a question but I googled and also checked my reference O'Reilly Learning Python book and I did not find a satisfactory answer. When I use readlines, what...
12
by: Yi Xing | last post by:
Hi All, I want to read specific lines of a huge txt file (I know the line #). Each line might have different sizes. Is there a convenient and fast way of doing this in Python? Thanks. Yi Xing
4
by: wscrsurfdude | last post by:
Ik have an uml file I want to read with readlines. I have the following code: infile = open("out2.txt","r") for line in infile.readlines(): print line The print statement just gives the data,...
2
by: cshirky | last post by:
Newbie question: I'm trying to turn a large XML file (~7G compressed) into a YAML file, and my program seems to be buffering the input. IOtest.py is just import sys for line in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.