Python os.walk() Method

Python os.walk() method is used to generate the file names in the file index tree by walking either top-down or bottom-up.

This function will help you traverse a directory. Each directory in the tree rooted at the top of the directory generates a 3-tuple: dirpath, dirnames, and filenames.

Syntax

os.walk(top, topdown=True, onerror=None, followlinks=False)

Parameters

  1. top − Each directory rooted at the directory generates three tuples, for example, (dirpath, dirnames, and filenames).
  2. topdown − This is an optional argument in which, if it is True or not specified, the directories are scanned from the top down. If the topdown is applied to False, directories are considered bottom-up.
  3. onerror − This is an optional argument showing an error to continue with a walk or raise the exception to abort the walk.
  4. followlinks − This visits directories pointed to by symlinks if set to True.

Return Value

It returns a 3-tuple (dirpath, dirnames, filenames).

Visual representation

Visual representation of os.walk() method in Python

Example 1: Usage of os.walk()

import os

if __name__ == "__main__":
 for (root, dirs, files) in os.walk('/Users/krunal/Desktop/code/pyt/database', 
                                    topdown=True):
 print("The root is: ")
 print(root)
 print("The directories are: ")
 print(dirs)
 print("The files are: ")
 print(files)
 print('--------------------------------')

Output

The root is:
/Users/krunal/Desktop/code/pyt/database

The directories are:
['.vscode']

The files are:
['shows.csv', 'Netflix.csv', 'marketing.csv', 'new_file.json', 
'data.json', 'Netflix', 'shows.db', 'app.py', 'purchase.csv', 
'final.zip', 'sales.csv']
--------------------------------
The root is:
/Users/krunal/Desktop/code/pyt/database/.vscode

The directories are:
[]

The files are:
['settings.json']
--------------------------------

Example 2: Passing topdown = False

import os

if __name__ == "__main__":
  for (root, dirs, files) in os.walk('/Users/krunal/Desktop/code/pyt/database', 
                                     topdown=False):
    print("The root is: ")
    print(root)
    print("The directories are: ")
    print(dirs)
    print("The files are: ")
    print(files)
    print('--------------------------------')

Output

The root is:
/Users/krunal/Desktop/code/pyt/database/.vscode

The directories are:
[]

The files are:
['settings.json']

--------------------------------
The root is:
/Users/krunal/Desktop/code/pyt/database

The directories are:
['.vscode']

The files are:
['shows.csv', 'Netflix.csv', 'marketing.csv', 'new_file.json', 'data.json', 
'Netflix', 'shows.db', 'app.py', 'purchase.csv', 'final.zip', 'sales.csv']

As you can see from the path that returns the path, a list of directories, and a list of files from the bottom-up. By default, errors from the listdir() function are ignored.

Example 3: Printing directory tree

import os


def print_directory_tree(startpath):
  for root, dirs, files in os.walk(startpath):
    level = root.replace(startpath, '').count(os.sep)
    indent = ' ' * 4 * level
    print(f"{indent}{os.path.basename(root)}/")
    subindent = ' ' * 4 * (level + 1)
    for f in files:
      print(f"{subindent}{f}")


startpath = '/Users/krunallathiya/Desktop/Code/pythonenv/env' # Your directory path
print_directory_tree(startpath)

Output

Output of Printing directory tree

Example 4: Searching for a specific file

You can search for a specific file by walking through all directories starting from a root directory.

import os


def find_file(root_path, file_name):
  for dirpath, dirnames, filenames in os.walk(root_path):
  if file_name in filenames:
    return os.path.join(dirpath, file_name)
  return None


root_path = '/Users/krunallathiya/Desktop/Code/pythonenv/env'
file_name = 'data.txt'

found_path = find_file(root_path, file_name)
if found_path:
  print(f"File found at: {found_path}")
else:
  print("File not found.")

Output

File found at: /Users/krunallathiya/Desktop/Code/pythonenv/env/data.txt

Example 5: Counting the number of files in a directory tree

import os

root_dir = '/Users/krunallathiya/Desktop/Code'
file_count = 0

for dirpath, dirnames, filenames in os.walk(root_dir):
  file_count += len(filenames)

print(f'Total number of files: {file_count}')

Output

Total number of files: 41574

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.