Re: Opening files without closing them

2006-03-08 Thread 3c273
"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)

Re: Opening files without closing them

2006-03-08 Thread 3c273
"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.

Re: Opening files without closing them

2006-03-07 Thread Bryan
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

Re: Opening files without closing them

2006-03-07 Thread Peter Hansen
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

Re: Opening files without closing them

2006-03-07 Thread 3c273
"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

Re: Opening files without closing them

2006-03-06 Thread Robert Kern
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

Re: Opening files without closing them

2006-03-06 Thread Paul Rubin
"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

Re: Opening files without closing them

2006-03-06 Thread 3c273
"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

Re: Opening files without closing them

2006-03-05 Thread Steven Bethard
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 clos

Re: Opening files without closing them

2006-03-05 Thread Robert Kern
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.

Re: Opening files without closing them

2006-03-05 Thread Erik Max Francis
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

Re: Opening files without closing them

2006-03-05 Thread Bruno Desthuilliers
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 c

Re: Opening files without closing them

2006-03-05 Thread Marcin Mielżyński
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

Re: Opening files without closing them

2006-03-05 Thread Robert Kern
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 clos

Re: Opening files without closing them

2006-03-05 Thread Marcin Mielżyński
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 clos

Opening files without closing them

2006-03-05 Thread Sandra-24
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 (aft