创建临时目录时出错

时间:2016-05-23 14:11:38

标签: python python-2.7

我尝试使用tempfile.mkdtemp()方法创建临时目录,但我收到以下错误

Traceback (most recent call last):
  File "src/main.py", line 29, in <module>
    tmp = tempfile.mkdtemp()
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/V
ersions/2.7/lib/python2.7/tempfile.py", line 331, in mkdtemp
    dir = gettempdir()
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/V
ersions/2.7/lib/python2.7/tempfile.py", line 275, in gettempdir
    tempdir = _get_default_tempdir()
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/V
ersions/2.7/lib/python2.7/tempfile.py", line 200, in _get_default_tem
pdir
    with _io.open(fd, 'wb', closefd=False) as fp:
AttributeError: 'module' object has no attribute 'open'

使用Python 2.7.6和2.7.11

运行此代码时
import errno
import shutil
import tempfile

tmp = tempfile.mkdtemp()
try:
    shutil.rmtree(tmp)
except OSError as e:
    if e.errno == errno.ENOENT:
        print "No such file or directory"

我做错了什么?

2 个答案:

答案 0 :(得分:1)

在目录中我有一个名为io.py的文件,重命名此文件可以解决由名称阴影陷阱引起的问题。

See more here,谢谢J.F. Sebastian

答案 1 :(得分:-1)

在python 2.7.11中运行确切的脚本时,我没有错误。也许你可以尝试打印临时文件夹的名称,以确保它被正确创建(在删除之前)