Menu Close

How to run mkdir -p functionality in Python?

Sometimes, we want to run mkdir -p functionality in Python.

in this article, we’ll look at how to run mkdir -p functionality in Python.

How to run mkdir -p functionality in Python?

To run mkdir -p functionality in Python, w ecan use the pathlib‘s mkdir method.

For instance, we write

import pathlib

pathlib.Path("/tmp/path/to/desired/directory").mkdir(parents=True, exist_ok=True)

to create a path object with

pathlib.Path("/tmp/path/to/desired/directory")

Then we call mkdir with parents and exist_ok both set to True to create the directory at /tmp/path/to/desired/directory regardless of whether the parent directory exist or not.

Conclusion

To run mkdir -p functionality in Python, w ecan use the pathlib‘s mkdir method.

Posted in Python, Python Answers