[Tutor] about pyhton + regular expression

Jeff Shannon jeff@ccvcorp.com
Thu Mar 20 12:40:01 2003


Michael Janssen wrote:

>both not. In recent version (otherwise while loop, correct) of Python, you
>can do:
>for line in open('file.txt'):
>     # process line
>
># modern spelling is:
>for line in file('file.txt'):
>     # process line
>
>
>readline() reads one line of the file. read() the whole file as a string.
>readlines() the whole file as a list of lines.
>  
>

It should also be noted that, in both new and older versions of Python, 
there's a special pseudo-iterator that can be used.  If you use the 
standard idiom

infile = open('file.txt')
for line in infile.readlines():
    [...]
infile.close()

then the entire contents of the file is read into a list of lines.  If 
the file is large, this may swamp available memory.  You can use the 
special xreadlines() method instead --

for line in infile.xreadlines():
    [...]

This uses a small amount of behind-the-scenes magic to only read a small 
amount of the file at once, so you can use this to comfortably iterate 
through files that are considerably larger than available memory.

Also note that I prefer to save a reference to my file-object and 
explicitly close it when done.  In general, a file is automatically 
closed when the file-object representing it is destroyed, so allowing 
this to happen implictly usually works -- but the catch here is 
"usually".  The problem is that file-objects aren't necessarily 
destroyed right away when the last reference for them is deleted -- it 
happens right away in the current implementation of CPython, but not in 
Jython, and it's not guaranteed in *any* implementation.  Typically, 
with files that are read once and never written to, this won't matter 
much, but if you're reading in a file and then later writing to the same 
file, it could cause problems.  I feel that it's a good habit to 
explicitly close files, whether strictly necessary or not, so that I 
don't have to worry about which circumstances may be problematic and 
which are safe.

Jeff Shannon
Technician/Programmer
Credit International