Search the web
Sign In
New User? Sign Up
python-list · Python List
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Message search is now enhanced, find messages faster. Take it for a spin.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
Re: open.read   Message List  
Reply | Forward Message #106144 of 122067 |
David Bear wrote:

> I was in a hurry to do something and I did
>
> stuff = open("filename", "r").read()
>
> then I realized, "hey I didn't close the file".
>
> so python close it automagically? Since I was doing python
> interactively I expect the interpreter closed the file for me but my
> guess is the above syntax is probably poor. any comments?

It's very common use in classic Python, and sometimes handy
for interactive or near-one-liner use, but in any "serious" program
explicitly closing the file just as soon as you're done with it is
beter than relying on the interpreter closing it for you -- it will
make your code more usable in Jython, and taken as a habit
it will prevent some cases (not -- directly -- the above one) where
even in CPython you might hang on to resources longer than
necessary. try/final is the way to GUARANTEE that some key
finalization code gets executed, by the way, in case you need that.


Alex

--
http://mail.python.org/mailman/listinfo/python-list



Thu Aug 21, 2003 1:23 pm

aleax@...
Send Email Send Email

Forward
Message #106144 of 122067 |
Expand Messages Author Sort by Date

... It's very common use in classic Python, and sometimes handy for interactive or near-one-liner use, but in any "serious" program explicitly closing the file...
Alex Martelli
aleax@...
Send Email
Aug 21, 2003
6:28 am

On Thursday 21 August 2003 01:49 pm, François Pinard wrote: ... Except that, since open is an alias (and NOT deprecated), it's exactly equivalent (IMHO) which...
Alex Martelli
aleaxit@...
Send Email
Aug 22, 2003
2:07 am

[Alex Martelli] ... We are not speaking of `open' deprecation. When I write "the above may more adequately be written", one should not read "the above must be...
François Pinard
pinard@...
Send Email
Aug 22, 2003
4:12 am

[Alex Martelli] ... In more recent versions of Python, the `open' built in function has been replaced by the `file' built in constructor, and `open' has been...
François Pinard
pinard@...
Send Email
Aug 22, 2003
9:11 am
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help