Re: accepting file path or file object?
andrea crotti wrote:
> 2012/11/5 Peter Otten <email@example.com>:
>> I sometimes do something like this:
>> 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
>> yield sys.stdin
>> 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
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
That behaviour allows arbitrary nesting of with-statements
fn = ...
with xopen(fn) as f1:
with xopen(f1) as f2:
with xopen(f2) as f3:
and the file may only be closed on the outermost level.
|All times are GMT. The time now is 07:50 AM.|
Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.