unp*_*680 19 python unit-testing stringio stubs
我需要存根tempfile并且StringIO看起来很完美.只有这一切都失败了:
In [1]: from StringIO import StringIO
In [2]: with StringIO("foo") as f: f.read()
--> AttributeError: StringIO instance has no attribute '__exit__'
Run Code Online (Sandbox Code Playgroud)
提供固定信息而不是读取具有不确定内容的文件的常用方法是什么?
phi*_*hag 34
StringIO模块早于with语句.由于StringIO 已经在Python 3中删除了,你可以使用它的替换,io.BytesIO:
>>> import io
>>> with io.BytesIO(b"foo") as f: f.read()
b'foo'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5537 次 |
| 最近记录: |