Anonymous file closing

Donn Cave donn at u.washington.edu
Mon Jun 14 13:36:58 EDT 2004


In article <mailman.902.1087070336.6949.python-list at python.org>,
 Sergey Krushinsky <lunarium at comail.ru> wrote:

> I think, I can figure an example of such 'dependance'. This code:
>     open(filename, 'w').write('spam ' + open(filename, 'r').read())
> does not save 'spam' plus the old file contents, as I expected firstly. 
> The result is just 'spam'. At the other hand, using file handles we can 
> make it work as expected.

True - I mean, I can see there's a problem here - but it really
is a different sort of problem.

When you combine these operations, you have an order of execution
problem that truncates your file before finalization becomes an
issue:  the leftmost open happens first.

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list