Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Will file be closed automatically in a "for ... in open..." statement? Date: Tue, 16 Feb 2016 20:16:01 +1100 Lines: 49 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de 56Kb3XJRoIgxzQw86PMbkAVI+buYalPsiQJhOJUJJ/DA== 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; '16,': 0.03; 'received:209.85.223': 0.03; 'context': 0.05; 'subject:file': 0.07; 'cc:addr:python-list': 0.09; '"w")': 0.09; 'behave': 0.09; 'garbage': 0.09; 'references,': 0.09; 'scripts,': 0.09; 'subject:skip:a 10': 0.09; 'example:': 0.10; 'python': 0.10; 'anyway': 0.11; '"for"': 0.16; '"with"': 0.16; '2016': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iterating': 0.16; 'line)': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:closed': 0.16; 'wrote:': 0.16; 'of.': 0.18; 'versions': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '(the': 0.22; 'feb': 0.23; 'references': 0.23; 'unlike': 0.23; 'header:In-Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; 'block,': 0.29; 'unlikely': 0.29; 'mention': 0.30; 'probably': 0.31; 'generally': 0.32; 'run': 0.33; 'point': 0.33; 'though.': 0.33; 'open': 0.33; 'case,': 0.34; 'tue,': 0.34; 'file': 0.34; 'this?': 0.34; 'received:google.com': 0.35; 'trouble': 0.35; 'important.': 0.35; 'but': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; "won't": 0.38; 'received:209': 0.38; 'why': 0.39; 'data': 0.39; "didn't": 0.39; 'some': 0.40; 'future': 0.60; 'close': 0.61; 'entire': 0.61; 'matter': 0.63; 'more': 0.63; 'managers': 0.63; 'talking': 0.67; 'guaranteed': 0.67; 'worth': 0.67; 'saw': 0.77; 'counts': 0.81; "'for'": 0.84; "'with'": 0.84; 'book:': 0.84; 'chrisa': 0.84; 'closing,': 0.84; 'everywhere.': 0.84; 'guaranteed.': 0.84; 'immediate.': 0.84; 'writing.': 0.84; 'to:none': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=dG0v0xIRAhbCHqptl3GfV8MFBvKFzlq27Z5QF5VW7MQ=; b=x1NL0kY+DJXgyscwBpHZzNF8Yt0T5XVzFXDbCDQsvOGkMRKPT8Lve5E6nYBDWk26r7 7j431Bpc0he+cc6+301/kxwgfn76p2m9uRBIedSnieVUvEAajYuIFRPHh9rBVaVTjCGM 6vEGSPJseJm8PfDwr9v4lvHFS+YvF6ne/oPCfruinTT1eC9XQCJt7SAdFXISABIgzljI qARS6DYLIak1aqSNWRGzCbNC8WebcMi048sUsqFLW+Y4ufb9VGps5re//upu/Yv2BuxK DIPi317DG41VKHV6MbQu0t3+lG1w7iv6485A3os5RdLB69toEhFC6PsZwL4eJUP/TEoj AgAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc:content-type; bh=dG0v0xIRAhbCHqptl3GfV8MFBvKFzlq27Z5QF5VW7MQ=; b=HawrbVAvw4BTpBSd0e+GIaXKoqvY2qkvFrNf3R3vMwoiffidRYpEDuMrINejg8cL8a bNt8NPTzTrO/OmQK3yxKMVn9mmMWA6QlBSEVE4OSMp2auyujIh2Qv03tA7LphQ/bcGwg 7+hwfN5VrOiam08ZxA4xGGYgetRKKFydXo07u8JNTkZbEJqCnEA61BVOtGq0AuMv5Zlx ao7Rg7E6BfU/VLOei1QDxB3Q9IB3tK5mry8/IW0OSlTWnicYk75pk/u9ZxzID2AODCeU phxv7mC5U5Mh//s8QF4QrthSQtcXBsim8SYBRnI14buwyGZVst9EJBLXuyUfmWF12bGl x8+w== X-Gm-Message-State: AG10YOT9IgZ5UIFfFU9I31AdK4fn+MqgEEqgeoHNgJd1jbip53JRqW93EZaZXnV8OXM1ACW0nWpPbOglS8V7Sw== X-Received: by 10.107.47.162 with SMTP id v34mr20199302iov.19.1455614161566; Tue, 16 Feb 2016 01:16:01 -0800 (PST) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21rc2 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:102994 On Tue, Feb 16, 2016 at 7:39 PM, wrote: > I know > > with open('foo.txt') as f: > ...do something... > > will close the file automatically when the "with" block ends. > > I also saw codes in a book: > > for line in open('foo.txt'): > ...do something... > > but it didn't mention if the file will be closed automatically or not when the "for" block ends. Is there any document talking about this? and how to know if a file is in "open" or not? > The file will be closed when the open file object is disposed of. That will happen at some point after there are no more references to it. You're guaranteed that it stays around for the entire duration of the 'for' loop (the loop keeps track of the thing it's iterating over), but exactly when after that is not guaranteed. In current versions of CPython, the garbage collector counts references, so the file will be closed immediately; but other Python interpreters, and future versions of CPython, may not behave the same way. So the file will *probably* be closed *reasonably* promptly, but unlike the "with" case, you have no guarantee that it'll be immediate. For small scripts, it probably won't even matter, though. You're unlikely to run out of file handles, and the only time it would matter is if you're opening, closing, and then reopening the file - for example: fn = input("Name a file to frobnosticate: ") with open(fn) as f: data = [] for line in f: data = frobnosticate(data, line) with open(fn, "w") as f: f.writelines(data) For this to work reliably, the file MUST be closed for reading before it's opened for writing. The context managers are important. But this is pretty unusual. Of course, since it's so little trouble to use the 'with' block, it's generally worth just using it everywhere. Why run the risk? :) ChrisA who often forgets to use 'with' anyway