os.walk - Recursively 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).
Reference - https://docs.python.org/2/library/os.html
Python Program -
#!/usr/bin/python3
#Python Program to Print Directory and Files Recursively
import os
def print_dir(path):
for dirName,subdirList,fileList in os.walk(path):
nw_path = dirName.split('/')
print("|",(len(nw_path))*"---","[",os.path.basename(dirName),"]")
for fl in fileList:
print("|",len(nw_path)*"---","->",str(fl))
if __name__ == "__main__":
path = input("Enter the directory path :-")
print_dir(path)
Output -
surendra@Surendra:~/workspace/Python_Programs$ python3 recurse_print.py
Enter the directory path :-/home/surendra/workspace
| ------------ [ workspace ]
| --------------- [ Python_Programs ]
| --------------- -> swap_adjecentpairs.py
|| --------------- -> list_methods.py
| ------------------ [ .git ]
| ------------------ -> HEAD
| ------------------ -> index
| ------------------ -> description
| ------------------ -> COMMIT_EDITMSG
| ------------------ -> config
| ------------------ -> packed-refs
| --------------------- [ info ]
| --------------------- -> exclude
| --------------------- [ logs ]
| --------------------- -> HEAD
| ------------------------ [ refs ]
| --------------------------- [ heads ]
| --------------------------- -> master
| --------------------------- [ remotes ]
| ------------------------------ [ origin ]
| ------------------------------ -> HEAD
| ------------------------------ -> master
| --------------------- [ branches ]
| --------------------- [ hooks ]
| --------------------- -> pre-applypatch.sample
| --------------------- -> pre-rebase.sample
| --------------------- -> commit-msg.sample
| --------------------- [ refs ]
Reference - https://docs.python.org/2/library/os.html
Python Program -
#!/usr/bin/python3
#Python Program to Print Directory and Files Recursively
import os
def print_dir(path):
for dirName,subdirList,fileList in os.walk(path):
nw_path = dirName.split('/')
print("|",(len(nw_path))*"---","[",os.path.basename(dirName),"]")
for fl in fileList:
print("|",len(nw_path)*"---","->",str(fl))
if __name__ == "__main__":
path = input("Enter the directory path :-")
print_dir(path)
Output -
surendra@Surendra:~/workspace/Python_Programs$ python3 recurse_print.py
Enter the directory path :-/home/surendra/workspace
| ------------ [ workspace ]
| --------------- [ Python_Programs ]
| --------------- -> swap_adjecentpairs.py
|| --------------- -> list_methods.py
| ------------------ [ .git ]
| ------------------ -> HEAD
| ------------------ -> index
| ------------------ -> description
| ------------------ -> COMMIT_EDITMSG
| ------------------ -> config
| ------------------ -> packed-refs
| --------------------- [ info ]
| --------------------- -> exclude
| --------------------- [ logs ]
| --------------------- -> HEAD
| ------------------------ [ refs ]
| --------------------------- [ heads ]
| --------------------------- -> master
| --------------------------- [ remotes ]
| ------------------------------ [ origin ]
| ------------------------------ -> HEAD
| ------------------------------ -> master
| --------------------- [ branches ]
| --------------------- [ hooks ]
| --------------------- -> pre-applypatch.sample
| --------------------- -> pre-rebase.sample
| --------------------- -> commit-msg.sample
| --------------------- [ refs ]
Comments