Python 3 OS Module Script to Remove all Files in a Directory With a Specific Extension Full Project For Beginners

Python 3 OS Module Script to Remove all Files in a Directory With a Specific Extension Full Project For Beginners

 

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

 

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

See also  Python 3 Tkinter Grocery or Kirana Shop Store Management System Using MySQL Database GUI Desktop App Full Project For Beginners

 

python app.py

 

 

So as you can see all the .txt files were deleted using this python script

Leave a Reply