I'm writing a program that needs a folder to be created in the user's Documents folder. It runs perfectly the first time it is executed but fails if it is executed more than once. I'm using the os.makedirs function and it fails because the folder already exists after the first execution. How do I skip the os.makedirs function if the folder already exists?
path = "/home/" + username + "/Documents/Keys"
try:
os.makedirs(path)
filename = "file%s.txt" % random
with open("/home/" + str(username) + "/Documents/Keys/" + filename, 'w') as f:
f.write(str(createkey) + " " + random + " " + d)
sys.exit()
except OSError:
print "There was a problem generating the key"
Expected Result: If the folder has already been created; it should skip over the os.makedirs function, and execute the rest of the code.