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!

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From MRAB <python@mrabarnett.plus.com>
Newsgroups comp.lang.python
Subject Re: A fun python CLI program for all to enjoy!
Date Sat, 7 May 2016 01:38:44 +0100
Lines 43
Message-ID <mailman.442.1462581532.32212.python-list@python.org> (permalink)
References <ngiq0u$brn$1@dont-email.me> <7e52b918-2087-f93f-43cb-3411c1cdc881@mrabarnett.plus.com> <mailman.436.1462566630.32212.python-list@python.org> <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>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de 6JDv/rh5VGdJlJBI5Hdv+QLcHwjryYdJ2jWpAwYEzYHg==
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'mrab': 0.05; 'f.close()': 0.07; 'closed.': 0.09; 'descriptor': 0.09; 'python': 0.10; 'subject:python': 0.14; 'close()': 0.16; 'dfs': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'improper': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'mistake.': 0.16; 'places.': 0.16; 'program?': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:program': 0.16; 'works"': 0.16; 'you."': 0.16; 'wrote:': 0.16; '>>>': 0.20; 'function,': 0.22; 'appears': 0.23; 'code.': 0.23; 'wrote': 0.23; 'written': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'loop,': 0.29; 'comments': 0.30; 'checked': 0.31; 'says': 0.32; 'received:84': 0.32; 'subject:all': 0.32; 'maybe': 0.33; 'know.': 0.34; 'file': 0.34; 'worked': 0.34; 'but': 0.36; 'closing': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'seem': 0.37; 'no,': 0.38; 'files': 0.38; "didn't": 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'close': 0.61; "you'll": 0.61; 'here:': 0.63; 'statement,': 0.66; '"it': 0.84; "'with'": 0.84; 'answer,': 0.84; 'ethan': 0.91; 'furman': 0.91; 'lucky': 0.93
X-CM-Score 0.00
X-CNFS-Analysis v=2.1 cv=bsGxfxui c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=L9H7d07YOLsA:10 a=9cW_t1CCXrUA:10 a=s5jvgZ67dGcA:10 a=IkcTkHD0fZMA:10 a=uPZiAMpXAAAA:8 a=AuOIvhtXjDt9BlZUCh8A:9 a=QEXdDO2ut3YA:10 a=svzibyHiZmA4t4YY0eFS:22
X-AUTH mrabarnett@:2500
User-Agent Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.0
In-Reply-To <ngjatb$7g9$1@dont-email.me>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <ec082837-051c-a2a5-2a58-6a0a3506c414@mrabarnett.plus.com>
X-Mailman-Original-References <ngiq0u$brn$1@dont-email.me> <7e52b918-2087-f93f-43cb-3411c1cdc881@mrabarnett.plus.com> <mailman.436.1462566630.32212.python-list@python.org> <ngj86q$ia$1@dont-email.me> <572D28CF.3080100@stoneleaf.us> <mailman.439.1462577357.32212.python-list@python.org> <ngjatb$7g9$1@dont-email.me>
Xref csiph.com comp.lang.python:108248

Show key headers only | 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