skbio.io.util.
open_file
(file, **kwargs)[source]¶Context manager for skbio.io.util.open()
.
State: Stable as of 0.4.0.
The signature matches open()
. This context manager will not close
filehandles that it did not create itself.
Examples
Here our input isn’t a filehandle and so f will get closed.
>>> with open_file(['a\n']) as f:
... f.read()
...
'a\n'
>>> f.closed
True
Here we provide an open file and so f will not get closed and neither will file.
>>> file = io.BytesIO(b'BZh91AY&SY\x03\x89\x0c\xa6\x00\x00\x01\xc1\x00\x00'
... b'\x108\x00 \x00!\x9ah3M\x1c\xb7\x8b\xb9"\x9c(H\x01'
... b'\xc4\x86S\x00')
>>> with open_file(file) as f:
... f.read()
...
'a\nb\nc\n'
>>> f.closed
False
>>> file.closed
False