【发布时间】:2014-03-20 15:39:58
【问题描述】:
我正在尝试在一个小 python 脚本中使用一些临时文件,并在使用 tempfile-module 时遇到问题:
测试代码
import tempfile
with tempfile.NamedTemporaryFile() as temp:
print temp.name
temp.write('Some data')
print temp.seek(0).read()
输出
s$ python tmpFileTester.py
/var/folders/cp/8kmr4fhs4l94y8fdc6g_84lw0000gn/T/tmpcDgrzF
追溯
(最近一次调用最后一次):文件“tmpFileTester.py”,第 5 行,在 print temp.seek(0).read() AttributeError: 'NoneType' object has no attribute 'read'
--> 这发生在装有 MacOS 10.9 和 Python 2.7.5 的 MacBook 上,“delete=False”也会发生。
有什么想法吗?这可能是一个菜鸟错误,但我找不到问题...
【问题讨论】: