How to create a directory in Python using os.mkdir() or os.makedirs()?
Published on Aug. 22, 2023, 12:17 p.m.
You can create a directory in Python using the os.mkdir()
or os.makedirs()
methods. The main difference between these two methods is that os.mkdir()
creates a single directory, while os.makedirs()
creates a full directory path with all of the intermediate directories.
Here’s an example of how to use os.mkdir()
to create a new directory:
import os
# Create a directory
os.mkdir('/path/to/new/directory')
And here’s an example of how to use os.makedirs()
to create a new directory path:
import os
# Create a directory path
os.makedirs('/path/to/new/directory')
Both of these methods will create the same directory at the specified location. If the directory already exists, a FileExistsError
will be raised.