[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

File not closed

On 2019-03-20, ast <none at gmail.com> wrote:
> Hello
> In the following snippet, a file is opened but
> without any variable referring to it.
> So the file can't be closed.
> [line.split(":")[0]
>   for line in open('/etc/passwd')
>   if line.strip() and not line.startswith("#")]
> What do you think about this practice ?

If it's a short-lived program, then it will always get closed when the
program terminates.  Otherwise, it will get (eventually) get closed
when the garbage collection system cleans up the orphan object.

For short, throw-away progams, that's fine.  For long running servers,
it's bad style.

Grant Edwards               grant.b.edwards        Yow! Hmmm ... A hash-singer
                                  at               and a cross-eyed guy were
                              gmail.com            SLEEPING on a deserted
                                                   island, when ...