6.2 Directory Operations

6.2 Directory Operations

In Python, directory operations refer to the manipulation and management of directories or folders on a file system. Directories are used to organize and store files in a hierarchical structure. Python provides several built-in functions and modules that allow you to perform various directory operations, such as creating, deleting, renaming, and navigating directories.

Creating Directories

To create a new directory in Python, you can use the os module, which provides a set of functions for interacting with the operating system. The os module contains a function called mkdir() that allows you to create a new directory. Here's an example:

import os

# Create a new directory
os.mkdir("my_directory")

In the above example, the mkdir() function is used to create a new directory called "my_directory" in the current working directory. If the directory already exists, a FileExistsError will be raised.

Deleting Directories

To delete a directory in Python, you can use the os module's rmdir() function. This function removes an empty directory from the file system. Here's an example:

import os

# Delete a directory
os.rmdir("my_directory")

In the above example, the rmdir() function is used to delete the "my_directory" directory. If the directory is not empty or does not exist, a FileNotFoundError will be raised.

If you want to delete a directory and all its contents, including subdirectories and files, you can use the shutil module's rmtree() function. Here's an example:

import shutil

# Delete a directory and its contents
shutil.rmtree("my_directory")

The rmtree() function recursively deletes a directory and all its contents. Use this function with caution, as it permanently deletes the directory and its contents.

Renaming Directories

To rename a directory in Python, you can use the os module's rename() function. This function allows you to change the name of a directory. Here's an example:

import os

# Rename a directory
os.rename("old_directory", "new_directory")

In the above example, the rename() function is used to rename the "old_directory" to "new_directory". If the directory does not exist, a FileNotFoundError will be raised.

Navigating Directories

Python provides the os module's chdir() function to change the current working directory. This function allows you to navigate to a different directory. Here's an example:

import os

# Change the current working directory
os.chdir("my_directory")

In the above example, the chdir() function is used to change the current working directory to "my_directory". If the directory does not exist, a FileNotFoundError will be raised.

To get the current working directory, you can use the os module's getcwd() function. This function returns a string representing the current working directory. Here's an example:

import os

# Get the current working directory
current_directory = os.getcwd()
print(current_directory)

The above example will print the current working directory to the console.

Listing Directory Contents

To list the contents of a directory, you can use the os module's listdir() function. This function returns a list of all the files and directories in the specified directory. Here's an example:

import os

# List directory contents
contents = os.listdir("my_directory")
print(contents)

In the above example, the listdir() function is used to list the contents of the "my_directory" directory. The function returns a list of file and directory names.

Checking if a Directory Exists

To check if a directory exists in Python, you can use the os module's path.exists() function. This function returns True if the specified directory exists, and False otherwise. Here's an example:

import os

# Check if a directory exists
exists = os.path.exists("my_directory")
print(exists)

In the above example, the path.exists() function is used to check if the "my_directory" directory exists. The function returns a boolean value indicating the existence of the directory.

Conclusion

In this section, we explored various directory operations in Python. We learned how to create, delete, rename, navigate, list contents, and check the existence of directories. Understanding these operations is essential for managing and organizing files and directories in your Python programs.