Categories
Python Answers

How to run mkdir -p functionality in Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *