Search code examples
pythonwindowsdirectorytemp

create a folder in the temporary directory of windows with Python and with a given name


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


Solution

  • 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