简体   繁体   中英

Python3.5 and os.makedirs() not working

In my python script is a method that should create parent directory for path defined in input. Script finishes without any exception, but no directory was created. I run python3.5 in Ubuntu 14.04.

def create_parent_directory(path):
    dir_name = os.path.dirname(path)
    if not os.path.exists(dir_name) and not dir_name == "":
        os.makedirs(dir_name)

I made a second script just with this method and the problem is still the same, so it really is this method that is faulty. It looks like os.path.exists(dir_name) is not working either.

Is my input the problem? The command I use when I want the directory to be created in same place as where the script lies is srip.py MyPath. Should I use something like scrip.py /home/bla/blabla/MyPath ? Or should I use relative path in my script?

The directory you're trying to create already exists.

os.path.dirname("/home/bla/blabla/MyPath") returns the directory that MyPath is in, that is, /home/bla/blabla . If your script lies in the directory /home/bla/blabla , and you want to create a MyPath directory in the same directory,

def create_parent_directory(path):
    if not os.path.exists(path) and not path == "":
        os.makedirs(path)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM