os.walk()
os.walk() generate the file names in a directory tree by walking the tree either top-down or bottom-up. For each directory in the tree rooted at directory top (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames).
Example:
Python3
# Driver function import os for (root,dirs,files) in os.walk( '/home/nikhil/Desktop/' , topdown = True ): print (root) print (dirs) print (files) print ( '--------------------------------' ) |
Output:
10 Python File System Methods You Should Know
While programming in any language, interaction between the programs and the operating system (Windows, Linux, macOS) can become important at some point in any developer’s life. This interaction may include moving files from one location to another, creating a new file, deleting a file, etc.