Tutorial

Manage Directory and File in Python

4 min read

Directories, also known as folders, are containers that hold files and other directories.

Files contain data that can be accessed, modified, and saved by a program. Python provides built-in functions and modules for managing files and directories, allowing developers to create, modify, read, write, delete, and move files and directories on a computer’s file system.

Understanding directory and file management is essential for many real-world applications, such as data processing, web scraping, and automation. In this context, Python provides a powerful and flexible toolset to manage directories and files efficiently.

How do we get the Current Directory and Change it in Python?

We can get the current working directory using the os module, which provides a set of functions for interacting with the operating system. The os module’s getcwd() function returns the current working directory as a string.

Here’s an example of how to use get current directory:

import os

current_dir = os.getcwd()
print("Current working directory:", current_dir)

Here, the os module is imported, and the getcwd() function is called to retrieve the current working directory. The current_dir variable is assigned the resulting string value, which is then printed to the console.

We can change the current working directory using the os module’s chdir() function. This function changes the current working directory to the specified path.

Here’s an example of how to use it:

import os

new_dir = '/path/to/new/directory'

os.chdir(new_dir)

# Check that the directory has been changed
current_dir = os.getcwd()
print("Current working directory:", current_dir)

Here, the os module is imported, and the chdir() function is called with the path of the new directory we want to change to. The current_dir variable is assigned the resulting string value, which is then printed to the console to verify that the directory has been changed.

List of Files and Directories in Python

In Python, we can list directories and files in a directory using 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 of how to use listdir:

import os

dir_path = '/path/to/directory'

# Get a list of all files and directories in the specified directory
dir_contents = os.listdir(dir_path)

# Print each item in the list
for item in dir_contents:
    print(item)

Here, the os module is imported, and the listdir() function is called with the path of the directory we want to list. The resulting list of files and directories is stored in the dir_contents variable, which is then printed to the console using a for loop.

How to Create a New Directory and Rename a Directory in Python?

We can create a new directory using the os module’s mkdir() function.

This function creates a new directory with the specified name in the current working directory.

Here’s an example of how to use mkdir function in Python:

import os

new_dir_name = 'new_directory'

# Create a new directory
os.mkdir(new_dir_name)

Here, the os module is imported, and the mkdir() function is called with the name of the new directory we want to create. The function creates a new directory with the specified name in the current working directory.

We can also rename a directory using the os module’s rename() function. This function renames a file or directory with the specified old name to the new name.

Here’s an example of how to use it to rename a directory:

import os

old_dir_name = 'old_directory'
new_dir_name = 'new_directory'

# Rename the directory
os.rename(old_dir_name, new_dir_name)

Here, the os module is imported, and the rename() function is called with the old name of the directory we want to rename (old_dir_name) and the new name we want to give it (new_dir_name). The function renames the directory with the specified old name to the new name.

Remove or Delete the Directory and Files in Python

We can remove a file or an empty directory using the os module’s remove() and rmdir() functions, respectively.

Here’s an example of how to use these functions:

import os

# Remove a file
file_path = '/path/to/file'
os.remove(file_path)

# Remove an empty directory
dir_path = '/path/to/directory'
os.rmdir(dir_path)

Here, the os module is imported, and the remove() function is called with the path of the file we want to remove. Similarly, the rmdir() function is called with the path of the empty directory we want to remove.

Are you new to Python programming? Learn Python with the help of the best Python tutor online.


Leave a Reply

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