Re: [Tutor] do I need f.close()
On Wed, 11 Jun 2008, dave selby wrote: The whole topic came up because I just finished reading 'learning python' 3rd edition OReilly as a refresher where there are multiple instances of suggesting that you do the exact opposite eg ... [line.rstrip() for line in open('myfile')] ... p361 for line in open('script1.py') ... p261 p276 where it is described as 'best practice' for reading files line by line etc ... But that's for use of an input file. The question was about writing. I wouldn't worry too much about closing file that was used as input, as I am about one for output, where you want to be careful that the file contents are left in a particular desired state. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] do I need f.close()
dave selby [EMAIL PROTECTED] wrote The whole topic came up because I just finished reading 'learning python' 3rd edition OReilly as a refresher where there are multiple instances of suggesting that you do the exact opposite eg ... LP is a tutorial book so does not always teach industry strength programming practice (in common with most tutorials!) [line.rstrip() for line in open('myfile')] ... p361 for line in open('script1.py') ... p261 p276 where it is described as 'best practice' for reading files line by line Its the common idiom and for reading files not too bad. I certainly use this sometimes but if its critical I will move the open outside the loop: f = open(...) for line in f: or [line.rstrip() for line in f] But thats just so that I can detect and correct missing files if necessary etc. Its not becauise of closure issues. I'm fairly happy about letting the os close read only files, its really for writing that you want to be explicit. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] do I need f.close()
On Thu, June 12, 2008 4:32 pm, Alan Gauld wrote: dave selby [EMAIL PROTECTED] wrote The whole topic came up because I just finished reading 'learning python' 3rd edition OReilly as a refresher where there are multiple instances of suggesting that you do the exact opposite eg ... LP is a tutorial book so does not always teach industry strength programming practice (in common with most tutorials!) [line.rstrip() for line in open('myfile')] ... p361 for line in open('script1.py') ... p261 p276 where it is described as 'best practice' for reading files line by line Its the common idiom and for reading files not too bad. I certainly use this sometimes but if its critical I will move the open outside the loop: f = open(...) for line in f: or [line.rstrip() for line in f] But thats just so that I can detect and correct missing files if necessary etc. Its not becauise of closure issues. I'm fairly happy about letting the os close read only files, its really for writing that you want to be explicit. Alan, will the file close, even if it was opened for writing, when the program ends? I know it stays open if you're interactive, but otherwise too? Marilyn Davis Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] do I need f.close()
Marilyn Davis [EMAIL PROTECTED] wrote happy about letting the os close read only files, its really for writing that you want to be explicit. Alan, will the file close, even if it was opened for writing, when the program ends? I know it stays open if you're interactive, but otherwise too? It will if python closes cleanly. If the os, shell or python session suffer any mishap then the file might not be closed and data may be lost. There are other dangers too with not closing writeable files. If we have a long program we may inadvertantly reuse the name and open a new file without having closed the old one. That can result in unpredictable errors. In the worst case it could lose/overwrite the entire original file. This can, in theory, affect readable files too but it much less likely. The best thing nowadays is to use the new with construct since it guarantees file closure. Or in older versions always wrap file handling code in a try/fuinally block with the closes in the finally block. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] do I need f.close()
Thanks for all your help guys, I am getting a strong consensus that f.close() should be used everywhere, reading files as well as writing files and not to rely on the PVM to do clean-up for you. The whole topic came up because I just finished reading 'learning python' 3rd edition OReilly as a refresher where there are multiple instances of suggesting that you do the exact opposite eg ... [line.rstrip() for line in open('myfile')] ... p361 for line in open('script1.py') ... p261 p276 where it is described as 'best practice' for reading files line by line etc ... Dave -- Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] do I need f.close()
On Tue, Jun 10, 2008 at 12:07 PM, dave selby [EMAIL PROTECTED] wrote: Hi All, Up to now I when I need to write some data to a file I have been purposely using close() f = open(conf, 'w') f.writelines(lines) f.close() Is it as safe to use the following open(conf, 'w').writelines(lines) ie no close() to flush the data, but also not assigned an object name so am I right in thinking that as the object is 'reclaimed' close() is automatically called ? My guess is that's probably correct, but it's also bad programming. It's like that old saying of my grandma's: If you open it, close it! -Wayne ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] do I need f.close()
On Tue, Jun 10, 2008 at 1:07 PM, dave selby [EMAIL PROTECTED] wrote: Hi All, Up to now I when I need to write some data to a file I have been purposely using close() f = open(conf, 'w') f.writelines(lines) f.close() Is it as safe to use the following open(conf, 'w').writelines(lines) I will do this with file *read* in code that is not intended to be production quality. For writes and production code I always call close explicitly. ie no close() to flush the data, but also not assigned an object name so am I right in thinking that as the object is 'reclaimed' close() is automatically called ? In the current implementation of CPython I believe that is correct. For Jython, it is not correct, as Jython uses a different (not reference counted) GC. Perhaps that is why I am scrupulous about closing files that I open for write; I'm pretty sure I have been burned by it and I did a lot of Jython coding a few jobs ago... Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] do I need f.close()
On Tue, Jun 10, 2008 at 10:07 AM, dave selby [EMAIL PROTECTED] wrote: Hi All, Up to now I when I need to write some data to a file I have been purposely using close() f = open(conf, 'w') f.writelines(lines) f.close() Is it as safe to use the following open(conf, 'w').writelines(lines) ie no close() to flush the data, but also not assigned an object name so am I right in thinking that as the object is 'reclaimed' close() is automatically called ? Cheers Dave If you're using Python 2.5, you can use the new with - with open(x.txt) as f: data = f.read() do something with data which takes care of closing and possible exception handling automagically. Fredrik Lundh has a good article about it: http://effbot.org/zone/python-with-statement.htm -- www.fsrtechnologies.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] do I need f.close()
dave selby wrote: Hi All, Up to now I when I need to write some data to a file I have been purposely using close() f = open(conf, 'w') f.writelines(lines) f.close() Is it as safe to use the following open(conf, 'w').writelines(lines) ie no close() to flush the data, but also not assigned an object name so am I right in thinking that as the object is 'reclaimed' close() is automatically called ? True. And not, imho, bad programming -- Bob Gailer 919-636-4239 Chapel Hill, NC ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] do I need f.close()
f = open(conf, 'w') f.writelines(lines) f.close() Is it as safe to use the following open(conf, 'w').writelines(lines) ie no close() to flush the data, but also not assigned an object name so am I right in thinking that as the object is 'reclaimed' close() is automatically called ? True. And not, imho, bad programming I have to disagree. The effect of the above line is sensitive to the behavior of the underlying implmentation. As Kent mentioned, CPython says that it'll call close() immediately when the count of the last reference to the file object goes to zero. But Jython and IronPython on the other hand make no such guarantees. This is one of those places where the behavior of the above code is, unfortunately, undefined in the language specification. We have to dig ourselves out of such swamps: we should avoid getting into trouble by explicit close() of the resource. Python 2.5's 'with' form addresses this problem. The documentation on file.close() has an example: http://www.python.org/doc/lib/bltin-file-objects.html#l2h-297 So the code above could become: ## with open(conf, 'w') as f: f.writelines(lines) ## You'll need to add the 'from __future__ import with_statement' at the top, since this is a Python 2.5 specific feature. The code is almost as short as the previous and it guarantees that f will be flushed and closed after the statement's done. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor