python mkdir to make folder with subfolder?

PythonOperating SystemDirectoryMkdir

Python Problem Overview


This works:

mkdir('folder')

but this doesn't

mkdir('folder/subfolder')

error:

WindowsError: [Error 3] The system cannot find the path specified: 'folder/subfolder'

Python Solutions


Solution 1 - Python

Try os.makedirs instead, if you want to create a tree of directories in one call.

Solution 2 - Python

I tried the above on Linux using Python 2.6.6, but had to ensure that the string ended with a '/' (or '', on Windows). E.g.

os.makedirs('folder/subfolder/')

Otherwise only 'folder' was created.

Solution 3 - Python

I think you want the os.makedirs() function, which can create intermediate directories.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionb7875787View Question on Stackoverflow
Solution 1 - PythonMatthew IselinView Answer on Stackoverflow
Solution 2 - PythonstevejView Answer on Stackoverflow
Solution 3 - PythondcrostaView Answer on Stackoverflow