Why no open(f, "w").write()?

Donn Cave donn at drizzle.com
Thu May 30 02:18:08 EDT 2002


Quoth "Delaney, Timothy" <tdelaney at avaya.com>:
| > From: VanL [mailto:news at lindbergs.org]
| > 
| > Ok, I've read the thread... but how is
| > 
| > open(f, 'w').write(stuff)
| > 
| > different than
| > 
| > lines = open(f).readlines()
| > 
| > in terms of bad/good programming practice?
| > 
| > I use that second construction quite frequently.
|
| It suffers from the same problems, but the symptoms aren't as bad.
|
| With the read version, the file may still hang around without being closed.
| This may prevent other processes (or the same process) from opening the
| file. Usually when the program exists the OS will close the file.

Certainly on UNIX, there is no such problem - nothing prevents a
file from being opened for read concurrently by many processes or
one process, at least that would be exercised by open(f).readlines()

The only external problem I can think of would be that the file
inode would survive, if something were to delete (unlink) its
directory entry (can you say "reference counting"?) and in some
improbable scenario the filesystem could run out of space.

More likely (though still not terribly likely) you could run out
of file descriptors.  That's an integer range, at least 0..255
but probably a lot more, but in a long-running program it could
be something to look out for.

	Donn Cave, donn at drizzle.com



More information about the Python-list mailing list