I am calling os.mkdir
to create a folder with a certain set of generated data. However, even though the path I specified has not been created, the os.mkdi
I also faced the same problem, specially, when the string 'test' contains the multiple directory name. So when 'test' contains the single directory -
if not os.path.exists(test):
try:
os.makedir(test)
except:
raise OSError("Can't create destination directory (%s)!" % (test))
If the 'test' contains multiple directory like '\dir1\dir2' then -
if not os.path.exists(test):
try:
os.makedirs(test)
except:
raise OSError("Can't create destination directory (%s)!" % (test))
You have a file there with the name test
. You can't make a directory with that exact same name.
For python 2.7
from distutils.dir_util import mkpath
mkpath(path)
Just check if the path exist. if not create it
import os
if not os.path.exists(test):
os.makedirs(test)