[Edu-sig] Teaching about files

Marilyn Davis marilyn at deliberate.com
Mon Nov 8 19:43:03 CET 2004


On Sun, 7 Nov 2004, Danny Yoo wrote:

> 
> 
> On Sun, 7 Nov 2004, Kent Johnson wrote:
> 
> > So my question is, am I missing something here? Is f.read(n) important?
> 
> Hi Kent,
> 
> The most common use for f.read(n) in my personal experience has been in
> conjunction with the 'md5' module on really large files.  I have sometimes
> done read(1), for character-by-character stuff.  But otherwise, I tend to
> use files as iterators.

I'm collecting some regression tests for our new mail system, which is
turning out to be a big hogpodge of interacting scripts, so, for each
test, I gather the output of my MTA log, as well as the input, as well
as other files that my various scripts write, and I gather any new
mail messages generated -- all into one file.

For this, or any time you want to copy one file to another, this is
really nice to type:

fp_out.write(fp_in.read())

> I try to deemphasize read() and readlines(), as sucking a whole file as a
> list isn't a technique that will scale well with large inputs.

Yes!  You don't want to do anything that causes iterations through the
data unless you need to -- if you need performance.

And even fp_in.read() isn't good if you don't want to hold the whole
file in memory.  I'm careful not to do that in production code.

I can't think of a reason to use readlines(), unless you wanted to do
some processing on the first line that depends on the contents of the
last line, or maybe you want to count the number of lines and output
that before you output the file contents??

I will be teaching file IO Wednesday night.  So I'm grateful for this
discussion.

Thank you and good luck.

Marilyn Davis


> 
> Good luck!
> 
> _______________________________________________
> Edu-sig mailing list
> Edu-sig at python.org
> http://mail.python.org/mailman/listinfo/edu-sig
> 

-- 



More information about the Edu-sig mailing list