Python 3 Glob Module Script to Remove all Files in a Directory or SubDirectory With a Specific Extension Using Pattern Matching

Python 3 Glob Module Script to Remove all Files in a Directory or SubDirectory With a Specific Extension Using Pattern Matching

Python 3 Glob Module Script to Remove all Files in a Directory or SubDirectory With a Specific Extension Using Pattern Matching

import os
import glob

files = glob.iglob('uploads/**/*.csv', recursive=True)

for f in files:
    try:
        os.remove(f)
    except OSError as e:
        print("Error: %s : %s" % (f, e.strerror))
Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment