473,672 Members | 2,539 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

file.readlines( ) question

vch
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()?
Jul 21 '05 #1
2 4148
vch wrote:
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()?


Yes, and you just mentioned it. .readlines reads the entire file into
memory at once, which can obviously be expensive if the file is large.

A for loop with .readline, of course, will work, but modern versions of
Python allow iteration over a file, which will read it line by line:

for line in aFile:
...

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
A wise man never loses anything if he have himself.
-- Montaigne
Jul 21 '05 #2
vch
Erik Max Francis wrote:
... modern versions of
Python allow iteration over a file, which will read it line by line:

for line in aFile:
...


Thanks! Just what I need.
Jul 21 '05 #3

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

Similar topics

9
29222
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 the error message:
4
3058
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be
6
1725
by: John Reese | last post by:
def uselessHash(filename): fp= open(filename) hash= 0 for line in fp: hash ^= hash(line.strip()) fp.close() # do I need this or is fp closed by ref count? return hash Consider the function above. Do I need the fp.close(), or will the file be closed automatically when fp goes out of scope and its
8
2341
by: nephish | last post by:
Hey there. i want to set a variable to represent the last line of a text file how do i do that? or even better, how do i create a list of the lines of a text file?
34
2798
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 happens if the number of lines is huge? I have a very big file (4GB) I want to read in, but I'm sure there must be some limitation to readlines and I'd like to know how it is handled by python. I am using it like this:
10
2160
by: bradfordh | last post by:
Hello everyone. I am not sure how hard of a question is, but I do know that I need some help if you can give it. What I want to do is read the lines in a text file and store each line into a variable. I believe that I can use readlines() to read the individual lines, but how would I store each line into a variable for further analysis? Thanks for any and all suggestions.
8
1994
by: bahoo | last post by:
Hi, I have a text file containing a single line of text, such as 0024 How should I read it into a "list"? I tried this, but the "join" did not work as expected. Any suggestions?
4
2381
by: planetmatt | last post by:
I am a Python beginner. I am trying to loop through a CSV file which I can do. What I want to change though is for the loop to start at row 2 in the file thus excluding column headers. At present I am using this statement to initiate a loop though the records: for line in f.readlines(): How do I start this at row 2?
7
1638
by: Shankarjee Krishnamoorthi | last post by:
Hi, I am new to Python. I am trying to do the following inp = open(my_file,'r') for line in inp: # Perform some operations with line if condition something: # Start re reading for that position again for line in inp:
0
8486
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8828
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8680
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7446
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6238
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5705
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4418
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2819
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.