Python OS Walk Recursive Examples

Python OS Walk Recursive Examples

in this blog post i will explain in detail about python OS.walk() method. OS.walk() generate the file names in a directory tree by walking the tree either top-down or bottom-up. OS.walk builtin function generates a 3-tuple for each one of the directories in the tree, including the root itself.

Python OS Walk Recursive traverse examplesas i shown in the diagram i have folder structure. i have root devops folder, in that folder have 3 directories dev1, dev2, dev3. In dev1 i have 2 more sub directories dev1-1 and dev1-2 and two text files dev1-1.txt and dev1-2.txt. In dev2 i have one sub directory called dev2-1 and i text file dev2-1.txt. In dev3 i don't have any sub-directories but i have one text file dev3-1.txt. Now by using above folder structure i will show list how to get all files, directories and sub directories with python os.walk() method.

Python os.walk() Example-1

import os
x=r'C:\Users\enaknar\Desktop\pycharm\devops'
for r, d, f in os.walk(x):
   print(r)

or

import os
x=r'C:\Users\enaknar\Desktop\pycharm\devops'
for root,dirs,file in os.walk(x):
   print(root)

in this python os.walk() example it will show you all directories and sub-directories from the root directory.

Output:

C:\Users\enaknar\Desktop\pycharm\devops
C:\Users\enaknar\Desktop\pycharm\devops\dev1
C:\Users\enaknar\Desktop\pycharm\devops\dev1\dev1-1
C:\Users\enaknar\Desktop\pycharm\devops\dev1\dev1-2
C:\Users\enaknar\Desktop\pycharm\devops\dev2
C:\Users\enaknar\Desktop\pycharm\devops\dev2\dev2-1
C:\Users\enaknar\Desktop\pycharm\devops\dev3

Python os.walk() Example-2

import os
x=r'C:\Users\enaknar\Desktop\pycharm\devops'
for r,d,f in os.walk(x):
   print(d)

Output:

['dev1', 'dev2', 'dev3']
['dev1-1', 'dev1-2']
[]
[]
['dev2-1']
[]
[]

Python os.walk() Example-3 with Recursive:

import os
x=r'C:\Users\enaknar\Desktop\pycharm\devops'
for r,d,f in os.walk(x):
   for i in d:
      print(i)

here it will print all the directories and sub directories names, but not with complete path from root. so to traverse a directory tree we can use python os.walk() method.

Output:

dev1
dev2
dev3
dev1-1
dev1-2
dev2-1

Python os.walk() Example-4 with Recursive:

import os
x=r'C:\Users\enaknar\Desktop\pycharm\devops'
for r,d,f in os.walk(x):
   for i in d:
      print(os.path.join(r,i))

in example 3 we didn't get a complete path of all directories and sub directories. In this example we can get complete path of all directories and sub directories. In this way we can traverse a tree in python using os.walk() method.

Output:

C:\Users\enaknar\Desktop\pycharm\devops\dev1
C:\Users\enaknar\Desktop\pycharm\devops\dev2
C:\Users\enaknar\Desktop\pycharm\devops\dev3
C:\Users\enaknar\Desktop\pycharm\devops\dev1\dev1-1
C:\Users\enaknar\Desktop\pycharm\devops\dev1\dev1-2
C:\Users\enaknar\Desktop\pycharm\devops\dev2\dev2-1

Example-5:

Print all files with os.walk() method Recursively 

import os
x=r'C:\Users\enaknar\Desktop\pycharm\devops'
for r,d,f in os.walk(x):
   for i in f:
      print(i)

with above python script you can print all the files in directories and sub directories, but not with complete path. It will print only the file names.

Output:

dev1-1.txt
dev2-2.txt
dev2-1.txt
dev3-1.txt

Example-6:

Print all files with complete path os.walk() method Recursively

import os
x=r'C:\Users\enaknar\Desktop\pycharm\devops'
for r,d,f in os.walk(x):
   for i in f:
      print(os.path.join(r,i))

it will print all files recursively with complete full path.

Output:

C:\Users\enaknar\Desktop\pycharm\devops\dev1\dev1-1.txt
C:\Users\enaknar\Desktop\pycharm\devops\dev1\dev2-2.txt
C:\Users\enaknar\Desktop\pycharm\devops\dev2\dev2-1.txt
C:\Users\enaknar\Desktop\pycharm\devops\dev3\dev3-1.txt

 

Leave a Reply

Your email address will not be published. Required fields are marked *