【问题标题】:create a folder in the temporary directory of windows with Python and with a given name使用 Python 并使用给定名称在 windows 的临时目录中创建一个文件夹
【发布时间】:2015-08-27 02:39:11
【问题描述】:

使用下面的代码我可以在 windows 操作系统的临时目录中创建一个文件夹

import os
import tempfile
tempfile.mkdtemp() 

~\\appdata\\local\\temp\\tmppelfyu'

使用此代码,我可以获取临时文件,命名并创建

sysTemp = tempfile.gettempdir()
myTemp = os.path.join(sysTemp, 'foo')
if not os.path.exists(myTemp):
    os.makedirs(myTemp)

我想知道是否有一种简单的方法可以在 Windows 操作系统的临时目录中创建一个具有给定名称的文件夹

【问题讨论】:

    标签: python directory temp


    【解决方案1】:

    你想找一个临时目录,检查是否已经有一个你想要的名字的文件夹,如果没有就创建它并使用它。

    根据这些说明,您的解决方案将尽可能简单,同时仍然是明确的,因此坚持 Python 之禅。

    【讨论】:

      【解决方案2】:
      import os
      import tempfile
      
      # update
      full_path = os.path.join(tempfile.gettempdir(), 'foo')
      
      try:
         os.mkdir(full_path)
      except OSError as e:
         if e.errno == 17:
             pass
      
      print os.path.isdir(full_path) # True
      

      【讨论】:

      • 不!抱歉,如果您运行您拥有的行: ~\\appdata\\local\\temp\\tmpckw1ro\\foo' 而不是 \\appdata\\local\\temp\\foo'
      猜你喜欢
      • 2017-03-06
      • 1970-01-01
      • 2016-06-25
      • 2021-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-06
      相关资源
      最近更新 更多