【发布时间】:2013-07-06 11:57:03
【问题描述】:
我正在尝试使用 tempfile 模块。 (http://docs.python.org/2.7/library/tempfile.html) 我正在寻找一个临时文件,我可以打开多次以获取多个流来读取它。
tmp = ...
stream1 = # get a stream for the temp file
stream2 = # get another stream for the temp file
我已经尝试了几个函数(TemporaryFile、NamedTemporaryFile、SpooledTemporaryFile)并使用了 fileno 方法左右,但我无法执行我正在寻找的。p>
知道我应该自己开课吗?
谢谢
> 更新
尝试打开文件时出错...
In [2]: t = tempfile.NamedTemporaryFile()
In [3]: t.write('abcdef'*1000000)
In [4]: t.name
Out[4]: 'c:\\users\\mike\\appdata\\local\\temp\\tmpczggbt'
In [5]: f = open(t.name)
---------------------------------------------------------------------------
IOError Traceback (most recent call last)
<ipython-input-6-03b9332531d2> in <module>()
----> 1 f = open(t.name)
IOError: [Errno 13] Permission denied: 'c:\\users\\mike\\appdata\\local\\temp\\tmpczggbt'
【问题讨论】:
-
这听起来像XY problem;你到底想在这里做什么?
-
我想要做的是:我获取一个 URL,如果它太大而不是字符串变量,我想将数据存储在一个临时文件中。然后我想从几个地方读取它,但不想使用相同的流。当我使用一个字符串时,我可以从这个字符串创建几个 StringIO 而不复制数据,我正在寻找相同的行为来获取多个流。