Directory Methods in Python

Python provides several methods for handling directories through the os module. These methods allow you to create, delete, and navigate through directories easily.

Creating a Directory

To create a new directory, you can use the os.mkdir() function. This function takes the name of the directory you want to create as an argument.

Example of Creating a Directory

# Creating a directory
        import os
        
        # Directory name
        directory_name = "new_directory"
        
        # Creating the directory
        os.mkdir(directory_name)
        print("Directory", directory_name, "created.")

Output

Directory new_directory created.

Removing a Directory

To remove an existing directory, you can use the os.rmdir() function. This function removes the specified directory, but the directory must be empty.

Example of Removing a Directory

# Removing a directory
        import os
        
        # Directory name to remove
        directory_to_remove = "new_directory"
        
        # Removing the directory
        os.rmdir(directory_to_remove)
        print("Directory", directory_to_remove, "removed.")

Output

Directory new_directory removed.

Listing Directory Contents

To list all the files and directories in a specified directory, you can use the os.listdir() function. This function returns a list of the entries in the directory.

Example of Listing Directory Contents

# Listing directory contents
        import os
        
        # Listing contents of the current directory
        directory_contents = os.listdir('.')
        print("Contents of the current directory:", directory_contents)

Output

Contents of the current directory: ['file1.txt', 'file2.txt', 'new_directory']

Changing the Current Working Directory

You can change the current working directory using the os.chdir() function. This function takes the path of the directory you want to change to as an argument.

Example of Changing the Current Working Directory

# Changing the current working directory
        import os
        
        # Changing to a new directory
        new_directory_path = "path_to_directory"
        os.chdir(new_directory_path)
        print("Current working directory changed to:", os.getcwd())

Output

Current working directory changed to: path_to_directory

Checking if a Directory Exists

You can check if a directory exists using the os.path.exists() function. This function returns True if the directory exists and False otherwise.

Example of Checking Directory Existence

# Checking if a directory exists
        import os
        
        # Directory name to check
        directory_name = "new_directory"
        
        # Checking existence
        if os.path.exists(directory_name):
            print("Directory", directory_name, "exists.")
        else:
            print("Directory", directory_name, "does not exist.")

Output

Directory new_directory does not exist.

Conclusion

Python's os module provides various methods for managing directories, including creating, removing, and listing directories, as well as checking their existence. These methods are essential for file management tasks in Python.