Skip to content →

How to Traverse a File System in Python

Being able to list files and directories is something that comes up very often. Python’s os module has a function called walk() that helps with navigating file systems.

For this example, I made a directory called my_data with the following structure:

my_data
├── dir1
│   ├── level2-file.txt
│   └── subdir1
│       └── subsubdir1
│           └── level4-file.txt
└── walk_example.py

There are 4 levels or directories.

  1. The root, my_data
  2. my_data/dir1
  3. my_data/dir1/subdir1
  4. my_data/dir1/subdir1/subsubdir1

And there are some files at various levels in the tree.

How to use os.walk

os.walk returns a generator, that creates a tuple of values (current_path, directories in current_path, files in current_path).

Sammy Vilar
import os

path = '/tmp/my_data'

for current_path, dirs_in_path, files_in_path in os.walk(path):
    print(f'current_path: {current_path}') 
    print(f'dirs_in_path: {dirs_in_path}') 
    print(f'files_in_path: {files_in_path}')
current_path: /tmp/my_data
dirs_in_path: ['dir1']
files_in_path: ['level1-file.txt', 'walk_example.py']
current_path: /tmp/my_data/dir1
dirs_in_path: ['subdir1']
files_in_path: ['level2-file.txt']
current_path: /tmp/my_data/dir1/subdir1
dirs_in_path: ['subsubdir1']
files_in_path: []
current_path: /tmp/my_data/dir1/subdir1/subsubdir1
dirs_in_path: []
files_in_path: ['level4-file.txt']

From the output above you can see that there is a cyclical pattern. It starts from the base directory (/tmp/my_data), lists all the directories and files, then recursively does into the directories until there are no my directories.

Published in Today I Learned