How to Delete Files Matching Pattern in Python?
Now, let's see tutorial of how to delete files matching pattern in python. I explained simply step by step python delete multiple files wildcard. We will use python delete files with wildcard. you can see python delete files matching pattern. So, let's follow few step to create example of python remove files wildcard.
In this tutorial, we will use remove() and glob() of "os" library to delete files with wildcard. so let's see below example and do it.
remove() will remove file on given path.
glob() will give you files path recursively.
So let's see bellow example:
I have created following files on files folder and remove files with ".txt" extension:
files/test.txt files/demo.txt files/first.pngmain.py
import os, glob # Getting All Files List fileList = glob.glob('files/*.txt', recursive=True) # Remove all files one by one for file in fileList: try: os.remove(file) except OSError: print("Error while deleting file") print("Removed all matched files!")Output
Removed Files Lists
It will help you....
Happy Pythonic Coding!