Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'elif': 0.04; 'context': 0.05; 'none:': 0.05; 'sys': 0.05; 'mode:': 0.07; 'subject:file': 0.07; 'open(file,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sys.stdout': 0.09; 'termination': 0.09; 'def': 0.10; '(the': 0.15; '"-"': 0.16; 'closed:': 0.16; 'closes': 0.16; 'mode)': 0.16; 'nesting': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'subject:object': 0.16; 'wrote:': 0.17; 'yield': 0.17; 'thanks,': 0.18; '>>>': 0.18; 'import': 0.21; 'statement': 0.23; 'this:': 0.23; 'allows': 0.25; 'pass': 0.25; 'header:User-Agent:1': 0.26; 'handling': 0.27; 'skip:@ 10': 0.27; 'header:X-Complaints-To:1': 0.28; 'behaviour': 0.29; 'implicitly': 0.29; 'maybe': 0.29; 'gets': 0.32; 'file': 0.32; 'handle': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'false': 0.35; 'sometimes': 0.35; 'subject:?': 0.35; 'something': 0.35; 'there': 0.35; 'add': 0.36; 'received:org': 0.36; 'but': 0.36; 'closing': 0.36; 'level.': 0.36; 'two': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'andrea': 0.84; 'otten': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: accepting file path or file object? Date: Mon, 05 Nov 2012 14:47:03 +0100 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084bcc5.dip.t-dialin.net User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352123213 news.xs4all.nl 6966 [2001:888:2000:d::a6]:53320 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32769 andrea crotti wrote: > 2012/11/5 Peter Otten <__peter__@web.de>: >> I sometimes do something like this: >> @contextmanager >> def xopen(file=None, mode="r"): >> if hasattr(file, "read"): >> yield file elif file == "-" or file is None: # add file=None handling >> if "w" in mode: >> yield sys.stdout >> else: >> yield sys.stdin >> else: >> with open(file, mode) as f: >> yield f > That's nice thanks, there is still the problem of closing the file > handle but that's maybe not so important if it gets closed at > termination anyway.. I think you misunderstood the behaviour of my context manager. The with- statement (the last two lines) in xopen() implicitly closes files that are opened by xopen(). If you pass a file name the file will be closed: >>> with xopen("xopen.py") as f: pass ... >>> f.closed True However, if you pass an existing file it will not be closed: >>> import sys >>> from xopen import xopen >>> with xopen(sys.stdout) as f: pass ... >>> f.closed False That behaviour allows arbitrary nesting of with-statements fn = ... with xopen(fn) as f1: with xopen(f1) as f2: with xopen(f2) as f3: pass and the file may only be closed on the outermost level.