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

后端 未结 10 2280
挽巷
挽巷 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:18

    Simple answer that does not require any additional import, does not suppress errors such as "permission denied", "no space left on device" etc., yet accepts that the directory may already exist:

    import os
    
    try:
        os.mkdir(dirname)
    except FileExistsError :
        pass
    except :
        raise
    

提交回复
热议问题