How to create a new directory in Python
How to create a new directory in Python.
In Python, you can easily create a new directory using the os
module or the pathlib
module. These modules provide functions and methods to interact with the file system and perform various operations, including creating directories.
In this tutorial, we will explore both methods and provide step-by-step instructions on how to create a new directory in Python.
Using the os
module
The os
module in Python provides a wide range of functions for interacting with the operating system. It includes functions to create directories, check file and directory existence, and perform other file-related operations.
To create a new directory using the os
module, follow these steps:
- Import the
os
module:
import os
Choose a name for your new directory. For example, let's say we want to create a directory called "my_directory".
Use the
os.mkdir()
function to create the new directory. Pass the desired directory name as an argument. For example:
os.mkdir("my_directory")
This will create a new directory named "my_directory" in the current working directory.
- If you want to create a directory in a specific location, provide the absolute or relative path to the
os.mkdir()
function. For example:
os.mkdir("/path/to/my_directory")
This will create a new directory named "my_directory" in the "/path/to/" directory.
- If you want to create multiple directories at once, you can use the
os.makedirs()
function. It creates all intermediate directories if they don't exist. For example:
os.makedirs("/path/to/my_directory")
This will create the directory structure "/path/to/my_directory" if it doesn't already exist.
- To check if a directory exists before creating it, you can use the
os.path.exists()
function. It returnsTrue
if the directory exists, andFalse
otherwise. For example:
if not os.path.exists("my_directory"):
os.mkdir("my_directory")
This will create the "my_directory" only if it doesn't already exist.
Using the pathlib
module
The pathlib
module was introduced in Python 3.4 and provides an object-oriented approach for working with paths and file system operations. It offers a more intuitive and concise way to create directories.
To create a new directory using the pathlib
module, follow these steps:
- Import the
pathlib
module:
from pathlib import Path
Choose a name for your new directory. For example, let's say we want to create a directory called "my_directory".
Create a
Path
object with the desired directory name. For example:
path = Path("my_directory")
- Use the
mkdir()
method of thePath
object to create the new directory:
path.mkdir()
This will create a new directory named "my_directory" in the current working directory.
- To create a directory in a specific location, provide the absolute or relative path when creating the
Path
object. For example:
path = Path("/path/to/my_directory")
path.mkdir()
This will create a new directory named "my_directory" in the "/path/to/" directory.
- Similar to the
os
module, you can use theexist()
method of thePath
object to check if a directory exists before creating it. For example:
path = Path("my_directory")
if not path.exists():
path.mkdir()
This will create the "my_directory" only if it doesn't already exist.
That's it! You now know how to create a new directory in Python using both the os
module and the pathlib
module. Choose the method that suits your needs and start organizing your files and directories programmatically.