skbio.io.util.open_file

skbio.io.util.open_file(file, **kwargs)[源代码]

的上下文管理器 skbio.io.util.open() .

状态:0.4.0稳定。

签名匹配 open() 。此上下文管理器不会关闭不是它自己创建的文件句柄。

示例

这里我们的输入不是文件句柄,因此 f 将会被关闭。

>>> with open_file(['a\n']) as f:
...     f.read()
...
'a\n'
>>> f.closed
True

在这里,我们提供一个打开的文件,因此 f 不会关闭,也不会关闭 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