Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'handler': 0.04; 'cpython': 0.05; 'interpreter': 0.05; 'method,': 0.07; 'python': 0.08; 'counting': 0.09; 'garbage': 0.09; 'handlers': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'am,': 0.12; 'context;': 0.16; 'wrote:': 0.16; 'header:In-Reply-To:1': 0.22; 'programs.': 0.23; 'opens': 0.23; 'expect': 0.26; 'example': 0.28; 'least': 0.30; "didn't": 0.30; 'subject:?': 0.30; 'does': 0.32; 'it.': 0.33; 'header:User-Agent:1': 0.33; 'to:addr:python-list': 0.33; 'object': 0.33; 'normally': 0.34; 'too': 0.34; 'header:X -Complaints-To:1': 0.34; 'operating': 0.35; 'however,': 0.35; 'file': 0.35; 'received:au': 0.36; 'uses': 0.36; 'reference': 0.37; 'but': 0.37; 'received:org': 0.37; 'problems': 0.38; 'subject:with': 0.38; 'skip:o 20': 0.38; 'open': 0.38; 'goes': 0.39; 'files': 0.39; 'subject:: ': 0.39; 'did': 0.39; 'to:addr:python.org': 0.40; 'most': 0.60; 'making': 0.65; 'thousands': 0.65; 'limit': 0.67; 'collection': 0.68; 'have.': 0.77; 'andrea': 0.84; 'habit': 0.84; 'subject:always': 0.84; 'subject:necessary': 0.84; 'received:110': 0.95 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Lie Ryan Subject: Re: Is a with on open always necessary? Date: Sat, 21 Jan 2012 22:38:44 +1100 References: <4F198BD1.4040301@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 110-175-240-90.static.tpgi.com.au User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:8.0) Gecko/20111124 Thunderbird/8.0 In-Reply-To: <4F198BD1.4040301@gmail.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 24 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1327145939 news.xs4all.nl 6843 [2001:888:2000:d::a6]:39942 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:19186 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.