Hello Everybody.

I am using Python for my code.

I am trying to create folders at the end of a path. My path is /xxx/xxxx/xxxx/TEMP.

I want to dynamicaly create folders after this path in my program. I want something like:

/xxx/xxxx/xxxx/TEMP/Folder1.
/xxx/xxxx/xxxx/TEMP/Folder2.
/xxx/xxxx/xxxx/TEMP/Folder3.

At the moment I am using makedirs but its not working. It says File alreay exists.

Any help would be appreciated.

Mohsin

makedirs() fails if the directory already exists. In this case you don't need to create the directory: simply catch the exception

try:
    os.makedirs(mypath)
except OSError:
    # either dir already exists or can't create.
    # we can check
    if not os.path.isdir(mypath):
        # dir doesn't exist, propagate the exception
        raise
# if we're here, the directory exists

Thank you very much Gribouillis. Its working.... :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.