Skip to content

Instantly share code, notes, and snippets.

Created March 10, 2017 03:42
  • Star 57 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Create a folder with Python
import os
def createFolder(directory):
if not os.path.exists(directory):
except OSError:
print ('Error: Creating directory. ' + directory)
# Example
# Creates a folder in the current directory called data
Copy link

eric-hedengren commented Jul 9, 2020

Isn't this all already done by

import os
os.mkdirs(directory, exist_ok = True)


exist ok = True checks for existence and makes dir created if not existing

OSErrors from the os package will do the error messaging ...

You're right @gwangjinkim. Just switch mkdirs to makedirs and it's good to go.

import os
os.makedirs(directory, exist_ok = True)

Copy link

@EricHedengren thanks! True! I always have to look this up anew.

Copy link

if i would like to create two folders in single directory how to do that

Copy link

@khu17jain Write two makedirs statements to the same location. For loop it if you have a lot.

Copy link

best code I have ever found, thx!

Copy link

Thank you!

Copy link

how to create a folder with name in format yyyy-mm-dd from any year to today

Copy link

thank you man ``

Copy link

Great! Thanks so much!

Copy link

Thank you, very useful and helped me understand the command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment