Walking a Directory Tree
os.walk(top, topdown=True, onerror=None, followlinks=False)
is a function from the os
module in Python that generates the file names in a directory tree by walking the tree either top-down or bottom-up. By default, os.walk()
generates the file names in a directory tree top-down. For each directory in the tree rooted at directory top (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames).
Here's an example of how you can use os.walk()
to print all the files in a directory and its subdirectories:
import os
## Print all files in a directory and its subdirectories
for root, dirs, files in os.walk('.'):
for file in files:
print(os.path.join(root, file))
This code will start at the current directory (indicated by '.') and recursively walk through all the subdirectories, printing the full path of each file it encounters.
Here's an other example where you can use os.walk()
to search for a specific file with a specific extension in a directory:
import os
def search_file(directory, file_extension):
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith(file_extension):
print(os.path.join(root, file))
search_file('.','.txt')
This will look for all the files with the '.txt' extension and print the full path of the file.
os.walk()
is a powerful function that can be used for many tasks such as searching for files, analyzing directory structures, and more.
It's worth noting that os.walk
is a generator, which means it generates the values on the fly, rather than keeping them all in memory. This makes it efficient for handling large directory trees.