Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #108248

Re: A fun python CLI program for all to enjoy!

From MRAB <python@mrabarnett.plus.com>
Newsgroups comp.lang.python
Subject Re: A fun python CLI program for all to enjoy!
Date 2016-05-07 01:38 +0100
Message-ID <mailman.442.1462581532.32212.python-list@python.org> (permalink)
References (3 earlier) <ngj86q$ia$1@dont-email.me> <572D28CF.3080100@stoneleaf.us> <mailman.439.1462577357.32212.python-list@python.org> <ngjatb$7g9$1@dont-email.me> <ec082837-051c-a2a5-2a58-6a0a3506c414@mrabarnett.plus.com>

Show all headers | View raw


On 2016-05-07 00:58, DFS wrote:
> On 5/6/2016 7:29 PM, Ethan Furman wrote:
>> On 05/06/2016 04:12 PM, DFS wrote:
>>> On 5/6/2016 4:30 PM, MRAB wrote:
>>
>>>>    If you don't want to use the 'with' statement, note that closing the
>>>> file is:
>>>>
>>>>            f.close()
>>>>
>>>>    It needs the "()"!
>>>
>>> I used close() in 1 place, but close without parens in 2 other places.
>>> So it works either way.  Good catch.
>>
>> No, it doesn't.  `f.close` simple returns the close function, it doesn't
>> call it.  The "it works" was simply because Python closed the files for
>> you later.
>>
>> Not a big deal in a small program like this, but still a mistake.
>
>
> Yes.
>
> Check out the answer by 'unutbu' here:
>
> http://stackoverflow.com/questions/1832528/is-close-necessary-when-using-iterator-on-a-python-file-object
>
> He says "I...checked /proc/PID/fd for when the file descriptor was
> closed. It appears that when you break out of the for loop, the file is
> closed for you."
>
If you read the comments for that answer, you'll find the explanation.

> Improper f.close didn't seem to affect any of the files my program wrote
> - and I checked a lot of them when I was writing the code.
>
> Maybe it worked because the last time the file was written to was in a
> for loop, so I got lucky and the files weren't truncated?  Don't know.
>
> Did you notice any other gotchas in the program?
>

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

A fun python CLI program for all to enjoy! DFS <nospam@dfs.com> - 2016-05-06 15:10 -0400
  Re: A fun python CLI program for all to enjoy! MRAB <python@mrabarnett.plus.com> - 2016-05-06 21:30 +0100
    Re: A fun python CLI program for all to enjoy! DFS <nospam@dfs.com> - 2016-05-06 19:12 -0400
      Re: A fun python CLI program for all to enjoy! Ethan Furman <ethan@stoneleaf.us> - 2016-05-06 16:29 -0700
        Re: A fun python CLI program for all to enjoy! DFS <nospam@dfs.com> - 2016-05-06 19:58 -0400
          Re: A fun python CLI program for all to enjoy! MRAB <python@mrabarnett.plus.com> - 2016-05-07 01:38 +0100
          Re: A fun python CLI program for all to enjoy! Stephen Hansen <me+python@ixokai.io> - 2016-05-06 23:03 -0700
          Re: A fun python CLI program for all to enjoy! Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-05-07 18:24 +1200
            Re: A fun python CLI program for all to enjoy! alister <alister.ware@ntlworld.com> - 2016-05-07 08:51 +0000
  Re: A fun python CLI program for all to enjoy! Peter Otten <__peter__@web.de> - 2016-05-07 15:59 +0200

csiph-web