Welcome folks today in this blog post we will be removing all files
with a specific extension
using os
module in python. All the full source code of the application is given below.
Get Started
In order to get started you need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 |
import os dir_name = "uploads" test = os.listdir(dir_name) for item in test: if item.endswith(".txt"): os.remove(os.path.join(dir_name, item)) |
Now as you can see inside my directory
uploads we have lots of different
extensions files as shown below
So here in the above script we are only matching the .txt
files and deleting it so if you execute this script by typing the below command as shown below
python app.py
So as you can see all the .txt
files were deleted using this python
script