Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > About open file for Read

Reply
Thread Tools

About open file for Read

 
 
moonhkt
Guest
Posts: n/a
 
      12-10-2012
Hi All

I am new in Python. When using open and then for line in f .

Does it read all the data into f object ? or read line by line ?


f=open(file, 'r')
for line in f:
if userstring in line:
print "file: " + os.path.join(root,file)
break
f.close()


moonhk
 
Reply With Quote
 
 
 
 
Dave Angel
Guest
Posts: n/a
 
      12-10-2012
On 12/10/2012 11:36 AM, moonhkt wrote:
> Hi All
>
> I am new in Python. When using open and then for line in f .
>
> Does it read all the data into f object ? or read line by line ?
>
>
> f=open(file, 'r')
> for line in f:
> if userstring in line:
> print "file: " + os.path.join(root,file)
> break
> f.close()
>
>
> moonhk


open() does not read the whole file into any object. There is buffering
that goes on in the C libraries that open() calls, but that should be
transparent to you for regular files.

When you ask for a line, it'll read enough to fulfill that request, and
maybe some extra that'll get held somewhere in the C runtime library.

You should look into the 'with' statement, to avoid that f.close().
That way the file will be closed, regardless of whether you get an
exception or not.

http://docs.python.org/2/reference/c....html#index-15

with open(file,. "r") as f:
for line in f:
etc.

BTW, since you're in version 2.x, you should avoid hiding the builtin
file object. Call it something like file_name, or infile_name.

--

DaveA

 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      12-10-2012
Dave Angel wrote:

> On 12/10/2012 11:36 AM, moonhkt wrote:
>> Hi All
>>
>> I am new in Python. When using open and then for line in f .
>>
>> Does it read all the data into f object ? or read line by line ?
>>
>>
>> f=open(file, 'r')
>> for line in f:
>> if userstring in line:
>> print "file: " + os.path.join(root,file)
>> break
>> f.close()
>>
>>
>> moonhk

>
> open() does not read the whole file into any object. There is buffering
> that goes on in the C libraries that open() calls, but that should be
> transparent to you for regular files.
>
> When you ask for a line, it'll read enough to fulfill that request, and
> maybe some extra that'll get held somewhere in the C runtime library.
>
> You should look into the 'with' statement, to avoid that f.close().
> That way the file will be closed, regardless of whether you get an
> exception or not.
>
> http://docs.python.org/2/reference/c....html#index-15
>
> with open(file,. "r") as f:
> for line in f:
> etc.
>
> BTW, since you're in version 2.x, you should avoid hiding the builtin
> file object. Call it something like file_name, or infile_name.
>


Python does a bit of buffering on its own (which is why you cannot mix file
iteration and .readline() calls):

>>> with open("tmp.txt", "w") as f: f.writelines("%s\n" % i for i in

range(10**6))
....
>>> f = open("tmp.txt")
>>> f.readline()

'0\n'
>>> f.tell()

2
>>> f.readline()

'1\n'
>>> f.tell()

4
>>> next(f) # a for-loop does this implicitly

'2\n'
>>> f.tell()

8196 # after a next() call or the first loop iteration
# part of the file is now in a buffer.
>>> f.readline()

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Mixing iteration and read methods would lose data
>>> f.seek(0, 2)
>>> f.tell()

6888890


This is Python 2, in Python 3 f.tell() would fail after a next(f) call, but
f.readline() continues to work.


 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      12-10-2012
On Mon, 10 Dec 2012 08:36:22 -0800, moonhkt wrote:

> Hi All
>
> I am new in Python. When using open and then for line in f .
>
> Does it read all the data into f object ? or read line by line ?


Have you read the Fine Manual?

http://docs.python.org/2/library/std...l#file-objects

If you have read it, and the answer is still not clear, then please tell
us so we can improve the documentation.

`for line in open(file, "r"):` does not read the entire file into memory
at once, it iterates over the file reading one line at a time.


--
Steven
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
File.read(fname) vs. File.read(fname,File.size(fname)) Alex Dowad Ruby 4 05-01-2010 08:20 AM
open a binary file-read bit-change bit-update same file Sniper Abandon Ruby 3 11-19-2009 12:49 PM
file.read() doesn't read the whole file Sreejith K Python 24 03-24-2009 12:20 PM
Re: how to open a file in some application using Tkinter i am usingTKINTER to create GUI application i want to know how to open a worddocument in open office or any other applicatio Fredrik Lundh Python 1 01-09-2008 10:40 AM
open and read functions (can't make 'read' work..) ferbar C Programming 9 09-25-2005 07:52 AM



Advertisments