创建临时目录时出错

2024-09-28 22:36:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用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'

使用python2.7.6和2.7.11运行此代码时

^{pr2}$

我做错什么了?在


Tags: inpylibusrlocallineframeworktempfile
2条回答

在我的目录中有一个名为io.py的文件,重命名该文件可以解决名称隐藏陷阱导致的问题。在

See more here,谢谢J.F. Sebastian

在Python2.7.11中运行完全相同的脚本时,没有出现任何错误。也许您可以尝试打印临时文件夹的名称,以确保它创建正确(在删除之前)

相关问题 更多 >