Skip to main content

How to iterate over files in a directory in Python

How to iterate over files in a directory in Python.

Here is a detailed step-by-step tutorial on how to iterate over files in a directory using Python.

Step 1: Import the necessary modules

To start, you need to import the os module, which provides a way to interact with the operating system. This module will allow you to access the files and directories in a given directory.

import os

Step 2: Specify the directory path

Next, you need to specify the path to the directory that you want to iterate over. You can either provide an absolute path (e.g., /path/to/directory) or a relative path (e.g., ./directory). Make sure the directory exists and has the necessary permissions.

directory = '/path/to/directory'

Step 3: Iterate over the files in the directory

Now, you can use the os.listdir() function to get a list of all the files and directories in the specified directory. Then, you can iterate over this list to process each file individually.

for filename in os.listdir(directory):
# Process the file
print(filename)

Step 4: Check if the item is a file

Within the loop, you might want to perform specific actions only on files and not directories. To do this, you can use the os.path.isfile() function to check if the current item in the loop is a file.

for filename in os.listdir(directory):
if os.path.isfile(os.path.join(directory, filename)):
# Process the file
print(filename)

Step 5: Get the full file path

If you need to access the files within the loop, you will need the full file path. You can use the os.path.join() function to join the directory path with the current filename in the loop.

for filename in os.listdir(directory):
if os.path.isfile(os.path.join(directory, filename)):
full_path = os.path.join(directory, filename)
# Process the file using the full_path
print(full_path)

Step 6: Filter files by extension

Sometimes, you might only want to process files with a specific file extension. You can use the str.endswith() method to filter the files based on their extension.

extension = '.txt'  # Specify the desired file extension

for filename in os.listdir(directory):
if filename.endswith(extension) and os.path.isfile(os.path.join(directory, filename)):
full_path = os.path.join(directory, filename)
# Process the file with the specified extension
print(full_path)

Step 7: Handle subdirectories recursively

If the directory contains subdirectories and you want to iterate over all the files in those subdirectories as well, you can use recursion. You can create a separate function that calls itself for each subdirectory found.

def iterate_files(directory):
for filename in os.listdir(directory):
if os.path.isfile(os.path.join(directory, filename)):
full_path = os.path.join(directory, filename)
# Process the file
print(full_path)
else:
subdirectory = os.path.join(directory, filename)
iterate_files(subdirectory) # Recursively call the function for subdirectories

# Call the function with the initial directory
iterate_files(directory)

That's it! You now have a detailed tutorial on how to iterate over files in a directory using Python. Feel free to customize and use these code examples based on your specific needs.