Python: Walk Directory, List Files

By Xah Lee. Date: . Last updated: .
os.walk(dirPath)
visit dirPath and EVERY nested sub directory of dirPath.

For each dir visit, starting with dirPath, returns a generator (a list-like thing), where each element is a tuple of this format:

currentDirPath
full path of current directory.
dirNames
list of subdir names (not full path) that are immediate children.
fileNames
list of file names (not full path) that are immediate children.

If the current dir doesn't have subdir or files, it'll be a empty list.

# example of using os.walk to list all files in a directory and all its subdirectories.

import os

input_path = "/Users/xah/web/xahlee_info/python/"

for dir_path, subdir_list, file_list in os.walk(input_path):
    print( "current dir is ", dir_path )
    print( "current subdir list ", subdir_list )
    print( "current file list: " )
    for fname in file_list:
        full_path = os.path.join(dir_path, fname)
        print(full_path)

# output sample:
# /Users/xah/web/xahlee_info/python/stateful_func.html
# /Users/xah/web/xahlee_info/python/function_def.html
# /Users/xah/web/xahlee_info/python/python3_traverse_dir.html
# more

Return Type of os.walk

the os.walk returns a generator object.

import os

print(os.walk("/home/"))
# <generator object walk at 0xb705561c>

print(type(os.walk("/home/")))
# <class 'generator'>

β€œgenerator” is like a list, but more efficient because it doesn't actually compute the result until needed. You can convert the result of generator into a list by calling list().

import os

print( list( os.walk("/home/") ) )
# [('/home/', [], [])]

Note: os.walk is new in Python 3, but also available in python 2.7.x. In older version of Python 2, you'll need to use os.path.walk. [see Python 2: Walk Directory, List Files]

Walk a Directory

Python, Read Write File