Re: Opening files without closing them
"Bryan" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > if i really want to handle the exception, then i handle it at a conceptually > "higher" level by wrapping it in an exception which is basically what some > higher-level routine would do anyways. > > > try: > f = open('file) > try: > # do something > finally: > f.close() > except IOError: > # handle exceptions > I like this idea also. Thanks to all who helped me understand.. Louis -- http://mail.python.org/mailman/listinfo/python-list
Re: Opening files without closing them
"Peter Hansen" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > > So this is better, though probably excessive in small scripts: > > try: > f = open('file') > except IOError: > # do something else > else: > try: > content = f.read() > finally: > f.close() > > This takes advantage of "else" on try statements, which executes only if > the except statement is not executed. > Thank you for your reply. I had forgotten that you could use 'else' in a 'try' statement. I like this solution. Thanks again. Louis -- http://mail.python.org/mailman/listinfo/python-list
Re: Opening files without closing them
Peter Hansen wrote: > 3c273 wrote: >> "Robert Kern" <[EMAIL PROTECTED]> wrote in message >> news:[EMAIL PROTECTED] >> >>> Paul Rubin wrote: >>> Say that the open is inside the try block. If the file can't be opened, then 'open' raises an exception, 'f' doesn't get set, and then the 'finally' clause tries to close f. f might have been previously bound to some other file (which still has other handles alive) and so the wrong file gets closed. >>> And even if 'f' wasn't bound to anything, you will get a NameError instead >> of >> >>> the exception that you're really interested in seeing. >> >> Thanks to both of you. So in order to be thorough, should I be doing: >> try: >> f=open('file') >> except: IOError: >> print 'doesn't exist' >> so_something_else_instead() >> >> try: >> contents = f.read() >> finally: >> f.close() > > Unfortunately, that would still have trouble if the first exception > handler was executed, since then you'd try read from f, which would fail > with another exception, and then you'd try to close f, and that would > probably fail and raise an exception that isn't caught anywhere. > > So this is better, though probably excessive in small scripts: > > try: > f = open('file') > except IOError: > # do something else > else: > try: > content = f.read() > finally: > f.close() > > This takes advantage of "else" on try statements, which executes only if > the except statement is not executed. > > -Peter > this is what i always do for files and other types of resources: if it's a low-level routine, i usually let any exceptions bubble up to a higher level routine that cares or knows what to do. f = open('file') try: # do something finally: f.close() if i really want to handle the exception, then i handle it at a conceptually "higher" level by wrapping it in an exception which is basically what some higher-level routine would do anyways. try: f = open('file) try: # do something finally: f.close() except IOError: # handle exceptions bryan -- http://mail.python.org/mailman/listinfo/python-list
Re: Opening files without closing them
3c273 wrote: > "Robert Kern" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > >>Paul Rubin wrote: >> >>>Say that the open is inside the try block. If the file can't be >>>opened, then 'open' raises an exception, 'f' doesn't get set, and then >>>the 'finally' clause tries to close f. f might have been previously >>>bound to some other file (which still has other handles alive) and so >>>the wrong file gets closed. >> >>And even if 'f' wasn't bound to anything, you will get a NameError instead > > of > >>the exception that you're really interested in seeing. > > > Thanks to both of you. So in order to be thorough, should I be doing: > try: > f=open('file') > except: IOError: > print 'doesn't exist' > so_something_else_instead() > > try: > contents = f.read() > finally: > f.close() Unfortunately, that would still have trouble if the first exception handler was executed, since then you'd try read from f, which would fail with another exception, and then you'd try to close f, and that would probably fail and raise an exception that isn't caught anywhere. So this is better, though probably excessive in small scripts: try: f = open('file') except IOError: # do something else else: try: content = f.read() finally: f.close() This takes advantage of "else" on try statements, which executes only if the except statement is not executed. -Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Opening files without closing them
"Robert Kern" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Paul Rubin wrote: > > Say that the open is inside the try block. If the file can't be > > opened, then 'open' raises an exception, 'f' doesn't get set, and then > > the 'finally' clause tries to close f. f might have been previously > > bound to some other file (which still has other handles alive) and so > > the wrong file gets closed. > > And even if 'f' wasn't bound to anything, you will get a NameError instead of > the exception that you're really interested in seeing. Thanks to both of you. So in order to be thorough, should I be doing: try: f=open('file') except: IOError: print 'doesn't exist' so_something_else_instead() try: contents = f.read() finally: f.close() Thanks again. Louis -- http://mail.python.org/mailman/listinfo/python-list
Re: Opening files without closing them
Paul Rubin wrote: > "3c273" <[EMAIL PROTECTED]> writes: > >>>f = open(file) >>>try: >>>contents = f.read() >>>finally: >>>f.close() >>> >> >>Pardon the newbie question, but could you explain why? I have been doing it >>the same way as the OP and would like to know the difference. Thank you. > > Say that the open is inside the try block. If the file can't be > opened, then 'open' raises an exception, 'f' doesn't get set, and then > the 'finally' clause tries to close f. f might have been previously > bound to some other file (which still has other handles alive) and so > the wrong file gets closed. And even if 'f' wasn't bound to anything, you will get a NameError instead of the exception that you're really interested in seeing. -- Robert Kern [EMAIL PROTECTED] "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: Opening files without closing them
"3c273" <[EMAIL PROTECTED]> writes: > > f = open(file) > > try: > > contents = f.read() > > finally: > > f.close() > > > Pardon the newbie question, but could you explain why? I have been doing it > the same way as the OP and would like to know the difference. Thank you. Say that the open is inside the try block. If the file can't be opened, then 'open' raises an exception, 'f' doesn't get set, and then the 'finally' clause tries to close f. f might have been previously bound to some other file (which still has other handles alive) and so the wrong file gets closed. -- http://mail.python.org/mailman/listinfo/python-list
Re: Opening files without closing them
"Erik Max Francis" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Note quite. The assignment of the resources to its variable needs to be > done before the try: > > f = open(file) > try: > contents = f.read() > finally: > f.close() > Pardon the newbie question, but could you explain why? I have been doing it the same way as the OP and would like to know the difference. Thank you. Louis -- http://mail.python.org/mailman/listinfo/python-list
Re: Opening files without closing them
Sandra-24 wrote: > I was reading over some python code recently, and I saw something like > this: > > contents = open(file).read() > > And of course you can also do: > > open(file, "w").write(obj) > > Why do they no close the files? Is this sloppy programming or is the > file automatically closed when the reference is destroyed (after this > line)? I usually use: > > try: > f = open(file) > contents = f.read() > finally: > f.close() > > But now I am wondering if that is the same thing. Which method would > you rather use? Why? In Python 2.5, you'll write:: with open(file) as f: contents = f.read() and Python will automatically close the file at the end of the with-statement. Observe: Python 2.5a0 (trunk:42857M, Mar 5 2006, 14:50:28) [MSC v.1310 32 bit (Intel)] on win32 >>> from __future__ import with_statement >>> with open('readme.txt') as f: ... contents = f.read() ... >>> f Of course, you have to wait until August or so for Python 2.5: http://www.python.org/peps/pep-0356.html STeVe -- http://mail.python.org/mailman/listinfo/python-list
Re: Opening files without closing them
Erik Max Francis wrote: > Robert Kern wrote: > >>>I usually use: >>> >>>try: >>> f = open(file) >>> contents = f.read() >>>finally: >>> f.close() >>> >>>But now I am wondering if that is the same thing. Which method would >>>you rather use? Why? >> >>Just keep doing what you are doing, please. > > Note quite. The assignment of the resources to its variable needs to be > done before the try: > > f = open(file) > try: > contents = f.read() > finally: > f.close() Yes, you are correct. -- Robert Kern [EMAIL PROTECTED] "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: Opening files without closing them
Robert Kern wrote: >> I usually use: >> >> try: >> f = open(file) >> contents = f.read() >> finally: >> f.close() >> >> But now I am wondering if that is the same thing. Which method would >> you rather use? Why? > > Just keep doing what you are doing, please. Note quite. The assignment of the resources to its variable needs to be done before the try: f = open(file) try: contents = f.read() finally: f.close() -- Erik Max Francis && [EMAIL PROTECTED] && http://www.alcyone.com/max/ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis Who, my friend, can scale heaven? -- _Gilgamesh_, ca. 3rd C. BC -- http://mail.python.org/mailman/listinfo/python-list
Re: Opening files without closing them
Sandra-24 a écrit : > I was reading over some python code recently, and I saw something like > this: > > contents = open(file).read() > > And of course you can also do: > > open(file, "w").write(obj) > > Why do they no close the files? Is this sloppy programming or is the > file automatically closed when the reference is destroyed (after this > line)? IIRC, the current CPython implementation takes care of closing file objects that are no longer referenced. But this may not be true of other implementations (think: Jython). > I usually use: > > try: > f = open(file) > contents = f.read() > finally: > f.close() > > But now I am wondering if that is the same thing. Not quite: >>> try: ... f = open('file_that_doesnt_exists.ext') ... finally: ... f.close() ... Traceback (most recent call last): File "", line 4, in ? NameError: name 'f' is not defined >>> > Which method would > you rather use? For a quick script, the simplest one. For production code, it depends too much on the context to give a definitive single answer. -- http://mail.python.org/mailman/listinfo/python-list
Re: Opening files without closing them
Marcin Mielżyński wrote: > Sandra-24 wrote: >> I was reading over some python code recently, and I saw something like >> this: >> >> contents = open(file).read() >> >> And of course you can also do: >> >> open(file, "w").write(obj) >> >> Why do they no close the files? Is this sloppy programming or is the >> file automatically closed when the reference is destroyed (after this >> line)? I usually use: >> >> try: >> f = open(file) >> contents = f.read() >> finally: >> f.close() >> > > this above is equivalent to: > > open(file){|f| > contents=f.read > } > > the logic taking care of everything is encapsulated in open. > > but can be done in less ruby way way :) > > > > lopex Oops I thought I was writing to c.l.ruby :D sorry for spam lopex -- http://mail.python.org/mailman/listinfo/python-list
Re: Opening files without closing them
Sandra-24 wrote: > I was reading over some python code recently, and I saw something like > this: > > contents = open(file).read() > > And of course you can also do: > > open(file, "w").write(obj) > > Why do they no close the files? Is this sloppy programming or is the > file automatically closed when the reference is destroyed (after this > line)? Both! Usually, the files will probably be closed *if* you are using CPython. However, there is absolutely no guarantee of this behavior. For example, Jython uses a different garbage collection scheme, and the files will *not* close immediately. Future versions of CPython may have different behavior, too. > I usually use: > > try: > f = open(file) > contents = f.read() > finally: > f.close() > > But now I am wondering if that is the same thing. Which method would > you rather use? Why? Just keep doing what you are doing, please. -- Robert Kern [EMAIL PROTECTED] "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: Opening files without closing them
Sandra-24 wrote: > I was reading over some python code recently, and I saw something like > this: > > contents = open(file).read() > > And of course you can also do: > > open(file, "w").write(obj) > > Why do they no close the files? Is this sloppy programming or is the > file automatically closed when the reference is destroyed (after this > line)? I usually use: > > try: > f = open(file) > contents = f.read() > finally: > f.close() > this above is equivalent to: open(file){|f| contents=f.read } the logic taking care of everything is encapsulated in open. but can be done in less ruby way way :) lopex -- http://mail.python.org/mailman/listinfo/python-list
Opening files without closing them
I was reading over some python code recently, and I saw something like this: contents = open(file).read() And of course you can also do: open(file, "w").write(obj) Why do they no close the files? Is this sloppy programming or is the file automatically closed when the reference is destroyed (after this line)? I usually use: try: f = open(file) contents = f.read() finally: f.close() But now I am wondering if that is the same thing. Which method would you rather use? Why? Thanks, Sandra -- http://mail.python.org/mailman/listinfo/python-list