Iterate through files inside folder

file_system_glob

Skill - iterate through files inside folder

Table of Contents

Skills Required

Please make sure to have all the skills mentioned above to understand and execute the code mentioned below. Go through the above skills if necessary for reference or revision


In this post, we will learn to iterate through files in a folder using glob module

get all filenames in a folder

# import the glob module
import glob

# specify the file we desire to delete
fPath = r'C:\Users\Nagasudhir\Documents\Static_Web_Projects\WRLDC Apps Dashboard'

# use glob to get the filenames of files in the folder
fNames = glob.glob(r'{0}\*'.format(fPath))

print(fNames)
'''
this will print
['C:\\Users\\Nagasudhir\\Documents\\Static_Web_Projects\\WRLDC Apps Dashboard\\assets', 
'C:\\Users\\Nagasudhir\\Documents\\Static_Web_Projects\\WRLDC Apps Dashboard\\cards.css', 
'C:\\Users\\Nagasudhir\\Documents\\Static_Web_Projects\\WRLDC Apps Dashboard\\cards.html', 
'C:\\Users\\Nagasudhir\\Documents\\Static_Web_Projects\\WRLDC Apps Dashboard\\index.css', 
'C:\\Users\\Nagasudhir\\Documents\\Static_Web_Projects\\WRLDC Apps Dashboard\\index.html', 
'C:\\Users\\Nagasudhir\\Documents\\Static_Web_Projects\\WRLDC Apps Dashboard\\index.js']
'''

As seen above, the full path of filenames will be returned.

get all filenames with a desired extension using pattern matching

here we are using pattern matching to filter only HTML files within a folder

# import the glob module
import glob

# specify the file we desire to delete
fPath = r'C:\Users\Nagasudhir\Documents\Static_Web_Projects\WRLDC Apps Dashboard'

# use glob to get the filenames of html files in the folder using pattern matching
fNames = glob.glob(r'{0}\*.html'.format(fPath))

print(fNames)
'''
this will print
['C:\\Users\\Nagasudhir\\Documents\\Static_Web_Projects\\WRLDC Apps Dashboard\\cards.html', 
'C:\\Users\\Nagasudhir\\Documents\\Static_Web_Projects\\WRLDC Apps Dashboard\\index.html']
'''

get all filenames including sub-folders files with ‘recursive’ option

As shown below, using recursive = True and in \**\ the search pattern we can get filenames from sub-folders also

# import the glob module
import glob

# specify the file we desire to delete
fPath = r'C:\Users\Nagasudhir\Documents\CSharp_Projects\EdnaMonitoring\src\EdnaMonitoring.App\Data'

# use glob to get the filenames of all .cs files in the folder using pattern matching
# notice that even sub-folders files are also included
fNames = glob.glob(r'{0}\**\*.cs'.format(fPath), recursive=True)

print(fNames)
'''
this will print
['C:\\Users\\Nagasudhir\\Documents\\CSharp_Projects\\EdnaMonitoring\\src\\EdnaMonitoring.App\\Data\\AppIdentityDbContext.cs', 
'C:\\Users\\Nagasudhir\\Documents\\CSharp_Projects\\EdnaMonitoring\\src\\EdnaMonitoring.App\\Data\\Configurations\\IctConfiguration.cs', 
'C:\\Users\\Nagasudhir\\Documents\\CSharp_Projects\\EdnaMonitoring\\src\\EdnaMonitoring.App\\Data\\Configurations\\MonitoringEntityConfiguration.cs', 
'C:\\Users\\Nagasudhir\\Documents\\CSharp_Projects\\EdnaMonitoring\\src\\EdnaMonitoring.App\\Data\\Configurations\\TransLineConfiguration.cs']
'''

Example - get the size of each file in a folder recursively using glob module

import glob
import os

folPath = r'C:\Users\Nagasudhir\Documents\Python Projects\ping_status_dump_creation'

for fPath in glob.glob("{0}\**\*".format(folPath), recursive=True):

	# get file size

	fSize = os.stat(fPath).st_size/1024

	print('size of {0} is {1}'.format(fPath, fSize))

Video

The video for this post can be seen here

Online Interpreter

Although we recommend to practice the above examples in Visual Studio Code, you can run these examples online at https://www.tutorialspoint.com/execute_python_online.php

References


Table of Contents

Comments