Python3 Os Walk
# Python3.x Python3 os.walk() Method
[ Python3 OS File/Directory Methods](#)
* * *
### Overview
The os.walk() method can create a generator that generates all files in the directory and its subdirectories.
The os.walk() method is used to output file names in a directory by walking the directory tree, either upwards or downwards.
The os.walk() method is a simple and easy-to-use file and directory traverser that can help us efficiently handle file and directory operations.
It works on Unix and Windows.
### Syntax
The syntax for the **walk()** method is as follows:
os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])
### Parameters
* **top** -- Each folder under the root directory (including itself) produces a 3-tuple (dirpath, dirnames, filenames) [folder path, folder name, file name].
* **topdown** -- Optional, if True or not specified, a directory's 3-tuple will be generated before any of its subfolders' 3-tuples (top-down traversal). If topdown is False, a directory's 3-tuple will be generated after any of its subfolders' 3-tuples (bottom-up traversal).
* **onerror** -- Optional, is a function; it is called with one argument, an OSError instance. After reporting the error, continue walking, or raise an exception to terminate walking.
* **followlinks** -- If set to true, visit directories through symbolic links.
### Return Value
Returns a generator.
### Example
The following example demonstrates the use of the walk() method:
## Example
#!/usr/bin/python3
import os
for root, dirs, files in os.walk(".", topdown=False):
for name in files:
print(os.path.join(root, name))
for name in dirs:
print(os.path.join(root, name))
The output of executing the above program is:
./.bash_logout ./amrood.tar.gz ./.emacs ./httpd.conf ./www.tar.gz ./mysql.tar.gz ./test.py ./.bashrc ./.bash_history ./.bash_profile ./tmp ./tmp/test.py
[ Python3 OS File/Directory Methods](#)
YouTip