os.mkdir(path) returns OSError when directory does not exist

后端 未结 10 2174
挽巷
挽巷 2020-12-08 09:54

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

相关标签:
10条回答
  • 2020-12-08 10:30

    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))  
    
    0 讨论(0)
  • 2020-12-08 10:34

    You have a file there with the name test. You can't make a directory with that exact same name.

    0 讨论(0)
  • 2020-12-08 10:36

    For python 2.7

    from distutils.dir_util import mkpath
    mkpath(path)
    
    0 讨论(0)
  • 2020-12-08 10:37

    Just check if the path exist. if not create it

    import os    
    if not os.path.exists(test):
        os.makedirs(test)
    
    0 讨论(0)
提交回复
热议问题