python novice python novice - 1 year ago 175
Python Question

os.mkdir under if not working python

I have the below code, the

is not working on mine. Compiling does not return any error, but running the code does not create folder.

def folder():
timenow ='%Y-%m-%d_%H%M%S')
folderpath = os.path.join(currentpath,"folder",str(timenow))
if os.path.exists(folderpath) == False:

101 101
Answer Source

Try this:

def folder():
    timenow ='%Y-%m-%d_%H%M%S')
    folderpath = os.path.join(currentpath, "folder", str(timenow))
    if not os.path.exists(folderpath):
        print 'Created:', folderpath


makedirs will create the required subdirectories, whereas mkdir can only create one directory. That said, you should've seen an exception.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download