There might be several real-time use cases where need to process the files of a given directory, for example, formatting the data of each file. In order to process it, we might need to open each and every file. In this article, we will see how to open all the files in a folder or directory recursively using Python.

Import the modules

To achieve this we use os & glob modules. The os module provides the functions for interacting with the operating system whereas the glob module is used to find files based on the specified patterns.

import os
import glob

Getting the file_paths list

Now, we need to get the list of file paths. We need to provide the folder path where the files are located. This path can be either a relative or an absolute path.

Using the glob function from the glob module along with the appropriate pattern we retrieve the file paths within a specified folder.

For example, * the pattern is used to open every file in a folder regardless of the file extension.

file_paths = glob.glob(os.path.join(folder_path, '*'))

The os.path.join function is used to have compatibility across different operating systems.

Iterate and open each file

Now, we have the list of file paths and the next step is to open each file and perform operations on it.

We can use the open function in Python to open a file in the required mode. e.g., read mode, write mode, etc. and perform operations on the file.

for file_path in file_paths:
    with open(file_path, 'r') as file:
        # Perform operations on the file

        contents = file.read()
        print(contents)

Following is the complete example:

import os
import glob

# Specify the folder path
folder_path = '/path/to/folder'

# Retrieve file paths
file_paths = glob.glob(os.path.join(folder_path, '*'))

# Iterate over file paths and open each file
for file_path in file_paths:
    with open(file_path, 'r') as file:
        # Perform operations on the file
        # Example: Read the file contents
        contents = file.read()
        print(contents)

Categorized in:

Tagged in: