0

using the code below i can create a folder in the temporary directory of windows OS

import os
import tempfile
tempfile.mkdtemp() 

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

with this code i can get the temp file, give a name and create

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

i wish to know if there is a simple way to create a folder with a given name in the temporary directory of windows OS

Jim G.
  • 15,141
  • 22
  • 103
  • 166
Gianni Spear
  • 7,033
  • 22
  • 82
  • 131

2 Answers2

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
Vidul
  • 10,128
  • 2
  • 18
  • 20
0

You want to find a tempdir, check whether there already is a folder with a name you want, create it if it's not and use it.

Given these instruction, your solution is as simple as it can be while still being explicit, thus sticking to Zen of Python.

pacholik
  • 8,607
  • 9
  • 43
  • 55