If you’ve ever needed to delete all files with a certain extension from a folder, you know it can be time-consuming to do it manually. But did you know that there’s a way to automate this process using the Python programming language?
In this blog post, we’ll show you how to write a simple Python script that will delete all files with a .txt extension from a specified folder.
How to delete all files with a specific extension from a folder using Python?
1) First, open your favorite text editor and create a new file called delete_files.py.
2) Next, we’ll need to import the os and glob modules. The os module provides functions for interacting with the operating system, while glob provides functions for finding matching filenames using wildcards. So go ahead and add the following lines of code to your delete_files.py file:
import os import glob
3) Now we’ll define two variables: target_folder and target_extension. The target_folder variable should be set to the path of the folder containing the files you want to delete, while target_extension should be set to the extension of the files you want to delete (without the dot). For example, if you wanted to delete all .txt files from the /Documents/ folder, your variables would look like this:
target_folder = '/Documents/' target_extension = 'txt'
4) Next, we’ll use the glob module to find all filenames in our target folder that match our target extension. We’ll store these filenames in a list called target_filenames. Add the following line of code to your delete_files.py file:
target_filenames = glob.glob(target_folder + '*.' + target_extension)
5) Finally, we’ll loop through our list of target filenames and call the os.remove() function on each one. This function will delete the file from our target folder. Add the following line of code to your delete_files.py file:
Code language: CSS (css)
for filename in target_filenames: os.remove(filename)
6) That’s it! Save your delete_files.py file and run it from the command line or terminal. You should see all of the .txt files in your target folder deleted.
Full Python script
Code language: Python (python)
import os import glob target_folder = '/Documents/' target_extension = 'txt' target_filenames = glob.glob(target_folder + '*.' + target_extension) for filename in target_filenames: os.remove(filename)
Another Python script to delete many files at once
Starting with the fact that I needed to delete all the (.txt) files in a few folders or all of its subfolders, which would take too long to do by hand, Python can help.
Change the root path of the folder you want the script to work on and the file extension of the files you want to delete, and then run the script. it works !!
Code language: Python (python)
# Delete all files ending with .*txt from 'mypath' and all of its subfolders # Can be in use for any batch-deletion of similar-extension-files import os import re import os.path pattern = ".*txt" mypath = "/Volumes/Mydir/books/" for root, dirs, files in os.walk(mypath): for file in filter(lambda x: re.match(pattern, x), files): os.remove(os.path.join(root, file))
Whether you’re trying to clean up a messy directory or automate some tedious task, Python can help! In this blog post, we showed you how to write a simple script that will delete all files with a certain extension from a specified folder. Give it a try next time you need to do some bulk file deletion!