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


Groups > comp.lang.python > #19186 > unrolled thread

Re: Is a with on open always necessary?

Started byLie Ryan <lie.1296@gmail.com>
First post2012-01-21 22:38 +1100
Last post2012-01-26 05:22 +1100
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Is a with on open always necessary? Lie Ryan <lie.1296@gmail.com> - 2012-01-21 22:38 +1100
    Re: Is a with on open always necessary? K Richard Pixley <rich@noir.com> - 2012-01-25 09:17 -0800
      Re: Is a with on open always necessary? Lie Ryan <lie.1296@gmail.com> - 2012-01-26 05:22 +1100

#19186 — Re: Is a with on open always necessary?

FromLie Ryan <lie.1296@gmail.com>
Date2012-01-21 22:38 +1100
SubjectRe: Is a with on open always necessary?
Message-ID<mailman.4905.1327145939.27778.python-list@python.org>
On 01/21/2012 02:44 AM, Andrea Crotti wrote:
> I normally didn't bother too much when reading from files, and for example
> I always did a
>
> content = open(filename).readlines()
>
> But now I have the doubt that it's not a good idea, does the file
> handler stays
> open until the interpreter quits?

It is not necessary most of the time, and most likely is not necessary 
for short-lived programs. The file handler stays open until the file 
object is garbage collected, in CPython which uses reference counting 
the file handler is closed when the last reference to the file object is 
deleted or goes out of context; in python implementations that uses 
garbage collection method, this is indeterministic.

It is only strictly necessary for programs that opens thousands of files 
in a short while, since the operating system may limit of the number of 
active file handlers you can have.

However, it is considered best practice to close file handlers; making 
it a habit will avoid problems when you least expect it.

[toc] | [next] | [standalone]


#19410

FromK Richard Pixley <rich@noir.com>
Date2012-01-25 09:17 -0800
Message-ID<JOWTq.5640$YC4.3793@newsfe19.iad>
In reply to#19186
On 1/21/12 03:38 , Lie Ryan wrote:
> It is only strictly necessary for programs that opens thousands of files
> in a short while, since the operating system may limit of the number of
> active file handlers you can have.

The number you're looking for is 20 on many unix systems.  That's all. 
20 concurrently open file descriptors.

Modern systems open that number up somewhat, or make it tailorable.  But 
the number is still much lower than you might expect.

--rich

[toc] | [prev] | [next] | [standalone]


#19416

FromLie Ryan <lie.1296@gmail.com>
Date2012-01-26 05:22 +1100
Message-ID<mailman.5083.1327515796.27778.python-list@python.org>
In reply to#19410
On 01/26/2012 04:17 AM, K Richard Pixley wrote:
> On 1/21/12 03:38 , Lie Ryan wrote:
>> It is only strictly necessary for programs that opens thousands of files
>> in a short while, since the operating system may limit of the number of
>> active file handlers you can have.
>
> The number you're looking for is 20 on many unix systems. That's all. 20
> concurrently open file descriptors.
>
> Modern systems open that number up somewhat, or make it tailorable. But
> the number is still much lower than you might expect.

 From what I can gather, Linux defaults to 1024, Windows 16384, and OSX 
256; I doubt many people would need to work with other OSes.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web