10

I'm wondering if any of you have created a function to create a folder within a given path.

For Example: NewFolder = ['/projects/Resources/backup_Folder']

Victor Aguilar
  • 119
  • 1
  • 1
  • 6

1 Answers1

17

Use the os library, specifically os.mkdir()

https://docs.python.org/2/library/os.html#os.mkdir

For example,

path = "/usr/temp/foo"
os.mkdir(path)

If the intermediate folders don't exists, use os.makedirs as per Peter Wood's comment

path = "/newfolder1/newfolder2/foo"
os.mkdir(path)
mmj
  • 4,509
  • 2
  • 35
  • 42
Adam Hughes
  • 9,853
  • 6
  • 52
  • 89
  • Thank you very much Adam! It took me some time to understand what I was doing. I'm new to programming less than a year. landed an internship with a data science team. So this is all a learning experience for me. You were able to solve my issue. – Victor Aguilar Mar 16 '17 at 20:25
  • Happy to help, good luck with your internship – Adam Hughes Mar 16 '17 at 21:24
  • 8
    This won't work if intermediate directories don't exist (e.g. if `/usr/temp` doesn't exist). You need [**`os.makedirs`**](https://docs.python.org/3/library/os.html#os.makedirs) – Peter Wood Aug 17 '18 at 15:16