top : Root Path of the directories topdown : True ( default ), the triple for a directory is generated before the triples for any of its subdirectories onerror : errors are ignored ( by default ) , or a callback function with one argument can be used. followlinks : False ( default) Set value to True to visit directories pointed to by symlinks, on systems that support them.
Create this directory structure or run the code below to create the same.
import os
drive='D:\\'
sub_path='my_dir\\'
os.mkdir(drive+sub_path) # create directory D:\my_dir
for i in range(3): # update the level required.
sub_path=sub_path+'my_dir'+str(i)+'\\'
path=drive + sub_path
print(path)
os.mkdir(path)
path_file=path+'my_file'+str(i)+'.txt'
print(path_file)
f = open(path_file, "w")
Let us check the returned data type. (code with output )
import os
path='D:\\my_dir\\my_dir0'
t=os.walk(path)
print(type(t)) # <class 'generator'>
We will get the path , directory and files. (code with outputs)