About open file for Read

M

moonhkt

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
 
D

Dave Angel

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/compound_stmts.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.
 
P

Peter Otten

Dave said:
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/compound_stmts.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):
range(10**6))
....
8196 # after a next() call or the first loop iteration
# part of the file is now in a buffer.Traceback (most recent call last):
6888890


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

Steven D'Aprano

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/stdtypes.html#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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top