Re: Q on explicitly calling file.close

2009-09-23 Thread jhermann
>     except: >         return 0 So wrong on so many levels... -- http://mail.python.org/mailman/listinfo/python-list

Re: Q on explicitly calling file.close

2009-09-11 Thread Gabriel Genellina
En Thu, 10 Sep 2009 08:26:16 -0300, David C. Ullrich escribió: On Wed, 9 Sep 2009 15:13:49 -0700 (PDT), r wrote: On Sep 9, 4:19 pm, Charles Yeomans wrote: I removed the except block because I prefer exceptions to error codes. how will the caller know an exception has occurred? What if l

Re: Q on explicitly calling file.close

2009-09-10 Thread David C . Ullrich
On Wed, 9 Sep 2009 15:13:49 -0700 (PDT), r wrote: >On Sep 9, 4:19 pm, Charles Yeomans wrote: >(snip:) >> Unfortunately, both of these simple templates have the following   >> problem -- if open fails, a NameError will be raised from the finally   >> block. > >(snip) >> I removed the except block

Re: Q on explicitly calling file.close

2009-09-09 Thread r
On Sep 9, 4:19 pm, Charles Yeomans wrote: (snip:) > Unfortunately, both of these simple templates have the following   > problem -- if open fails, a NameError will be raised from the finally   > block. (snip) > I removed the except block because I prefer exceptions to error codes. how will the c

Re: Q on explicitly calling file.close

2009-09-09 Thread Charles Yeomans
On Sep 9, 2009, at 4:50 PM, r wrote: On Sep 9, 3:18 pm, David C Ullrich wrote: (snip) These days I've actually got the syntax and spelling memorized - I can type "close()" without needing to look it up! +1 You are so right David! I think some people around here need to look up "code reuse"

Re: Q on explicitly calling file.close

2009-09-09 Thread MRAB
r wrote: [snip] #-- Double Extra Creidit --# Create a backup_file() function that takes as arg and creates a copy of the file with the extension ".bak"... backup_file('C:\test.txt') -> 'C:\test.bak' You should've used raw strings. :-) -- http://mail.python.org/mailman/listinfo/python-list

Re: Q on explicitly calling file.close

2009-09-09 Thread r
On Sep 9, 3:18 pm, David C Ullrich wrote: (snip) > These days I've actually got the syntax and spelling memorized - > I can type "close()" without needing to look it up! +1 You are so right David! I think some people around here need to look up "code reuse". Here are a couple of simple templates

Re: Q on explicitly calling file.close

2009-09-09 Thread David C Ullrich
On Sat, 05 Sep 2009 23:41:08 +, Steven D'Aprano wrote: > On Sat, 05 Sep 2009 16:14:02 +, kj wrote: > >> Finally, I was under the impression that Python closed filehandles >> automatically when they were garbage-collected. [...] > > (3) For quick and dirty scripts, or programs that only u

Re: Q on explicitly calling file.close

2009-09-07 Thread Gabriel Genellina
En Sun, 06 Sep 2009 21:20:52 -0300, Stephen Hansen escribió: On Sun, Sep 6, 2009 at 4:31 PM, r wrote: On Sep 6, 1:14 pm, "Jan Kaliszewski" wrote: > 05-09-2009 r wrote: > > i find the with statement (while quite useful in general > > practice) is not a "cure all" for situations that need a

Re: Q on explicitly calling file.close

2009-09-06 Thread Stephen Hansen
On Sun, Sep 6, 2009 at 4:31 PM, r wrote: > On Sep 6, 1:14 pm, "Jan Kaliszewski" wrote: > > 05-09-2009 r wrote: > > > i find the with statement (while quite useful in general > > > practice) is not a "cure all" for situations that need and exception > > > caught. > > > > In what sense? > > *ahem

Re: Q on explicitly calling file.close

2009-09-06 Thread r
On Sep 6, 1:14 pm, "Jan Kaliszewski" wrote: > 05-09-2009 r wrote: > > i find the with statement (while quite useful in general > > practice) is not a "cure all" for situations that need and exception > > caught. > > In what sense? *ahem*! in the sense that the with statement (while quite useful

Re: Q on explicitly calling file.close

2009-09-06 Thread Jan Kaliszewski
05-09-2009 r wrote: i find the with statement (while quite useful in general practice) is not a "cure all" for situations that need and exception caught. In what sense? I think that: with open(...) as f: foo... is equivalent to: f = open(...) try: foo... finally:

Re: Q on explicitly calling file.close

2009-09-06 Thread Stephen Hansen
> > It's just too bad that 'with' doesn't support multiple separate "x as y" >> clauses. >> > > The developers already agreed with you ;-). > > "With more than one item, the context managers are processed as if multiple > with statements were nested: > > with A() as a, B() as b: >suite > is equ

Re: Q on explicitly calling file.close

2009-09-06 Thread Terry Reedy
Stephen Hansen wrote: This is precisely why the with statement exists; to provide a cleaner way to wrap a block in setup and teardown functions. Closing is one. Yeah, you get some extra indentation-- but you sorta have to live with it if you're worried about correct code. I think it's a good c

Re: Q on explicitly calling file.close

2009-09-05 Thread Stephen Hansen
On Sat, Sep 5, 2009 at 6:51 PM, kj wrote: > In <02b2e6ca$0$17565$c3e8...@news.astraweb.com> Steven D'Aprano < > st...@remove-this-cybersource.com.au> writes: > > >(3) For quick and dirty scripts, or programs that only use one or two > >files, relying on the VM to close the file is sufficient (alt

Re: Q on explicitly calling file.close

2009-09-05 Thread Steven D'Aprano
On Sun, 06 Sep 2009 01:51:50 +, kj wrote: > In <02b2e6ca$0$17565$c3e8...@news.astraweb.com> Steven D'Aprano > writes: > >>(3) For quick and dirty scripts, or programs that only use one or two >>files, relying on the VM to close the file is sufficient (although lazy >>in my opinion *wink*) >

Re: Q on explicitly calling file.close

2009-09-05 Thread kj
In <02b2e6ca$0$17565$c3e8...@news.astraweb.com> Steven D'Aprano writes: >(3) For quick and dirty scripts, or programs that only use one or two >files, relying on the VM to close the file is sufficient (although lazy >in my opinion *wink*) It's not a matter of laziness or industriousness, but

Re: Q on explicitly calling file.close

2009-09-05 Thread Steven D'Aprano
On Sat, 05 Sep 2009 16:14:02 +, kj wrote: > Finally, I was under the impression that Python closed filehandles > automatically when they were garbage-collected. (In fact (3) suggests > as much, since it does not include an implicit call to fh.close.) If so, > the difference between (1) and (3

Re: Q on explicitly calling file.close

2009-09-05 Thread r
On Sep 5, 2:47 pm, Dennis Lee Bieber wrote: (snip) > > Finally, I was under the impression that Python closed filehandles > > automatically when they were garbage-collected.  (In fact (3) > > suggests as much, since it does not include an implicit call to > > fh.close.) If so, the difference betwe

Re: Q on explicitly calling file.close

2009-09-05 Thread Tim Chase
CPython uses reference counting, so an object is garbage collected as soon as there are no references to it, but that's just an implementation detail. Other implementations, such as Jython and IronPython, don't use reference counting, so you don't know when an object will be garbage collected, wh

Re: Q on explicitly calling file.close

2009-09-05 Thread r
On Sep 5, 1:17 pm, Dave Angel wrote: > kj wrote: > > There's something wonderfully clear about code like this: > > >     # (1) > >     def spam(filename): > >         for line in file(filename): > >             do_something_with(line) > > > It is indeed pseudo-codely beautiful.  But I gather that

Re: Q on explicitly calling file.close

2009-09-05 Thread Dave Angel
kj wrote: There's something wonderfully clear about code like this: # (1) def spam(filename): for line in file(filename): do_something_with(line) It is indeed pseudo-codely beautiful. But I gather that it is not correct to do this, and that instead one should do som

Re: Q on explicitly calling file.close

2009-09-05 Thread MRAB
kj wrote: There's something wonderfully clear about code like this: # (1) def spam(filename): for line in file(filename): do_something_with(line) It is indeed pseudo-codely beautiful. But I gather that it is not correct to do this, and that instead one should do

Q on explicitly calling file.close

2009-09-05 Thread kj
There's something wonderfully clear about code like this: # (1) def spam(filename): for line in file(filename): do_something_with(line) It is indeed pseudo-codely beautiful. But I gather that it is not correct to do this, and that instead one should do something l