How to Easily Filter and Rename Files Using a Blacklist of Keywords in Python
Автор: vlogize
Загружено: 2025-05-27
Просмотров: 0
Learn how to implement a simple Python script that filters and renames files based on a blacklist of unwanted phrases or keywords stored in a text file.
---
This video is based on the question https://stackoverflow.com/q/66538104/ asked by the user '999LAZARUS999' ( https://stackoverflow.com/u/15356873/ ) and on the answer https://stackoverflow.com/a/66582341/ provided by the user 'Bijay Regmi' ( https://stackoverflow.com/u/8591711/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Comparing content of a textfile with Filenames in the order to rename / filter not wanted phrases/keywords
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
How to Easily Filter and Rename Files Using a Blacklist of Keywords in Python
Renaming files can sometimes become a tedious task, especially when dealing with unwanted keywords or phrases embedded in the filenames. Suppose you have a lot of files containing colors like "Red," "Green," or "Blue," and you wish to remove these terms from the filenames. Instead of going through each file manually, you can automate this process using a simple Python script leveraging a blacklist of keywords. In this guide, we'll walk you through how to implement such a script step by step.
Understanding the Problem
You might find yourself in a situation where you need to clean up a directory of files by removing certain keywords from their names. This could be due to branding guidelines or simply personal preferences. In our example, you'll want to remove the keywords "Red," "Green," "Blue," and others from your filenames.
Setting Up Your Environment
Before diving into the code, make sure you have Python installed on your computer. You will also need a text file (data.txt) where you will define the keywords you want to filter out. The structure of data.txt should look like this:
[[See Video to Reveal this Text or Code Snippet]]
The Code Explained
Here's the complete Python script that accomplishes the task of renaming files based on a blacklist:
[[See Video to Reveal this Text or Code Snippet]]
Breakdown of the Code
Reading the Blacklist:
The read_text_file function opens the data.txt file, reads each line, and stores the keywords into a list called blacklist. The strip() method is used to remove any leading or trailing whitespace.
Replacing Keywords:
The function replace_blacklist takes the full filepath of a file and replaces each occurrence of any keyword in the BLACKLIST with an empty string. This effectively removes the keywords from the filename.
Renaming Files:
The script checks each file in the specified directory (PATH). If the filename contains any keyword from the BLACKLIST, it generates a new filename without those keywords and renames the file accordingly.
How to Run the Script
Prepare the data.txt file as explained above.
Set the PATH variable in the script to the directory containing the files you want to rename.
Run the Script by executing it in a Python environment.
Conclusion
By following the steps outlined above, you can easily automate the renaming of files based on a predefined list of unwanted keywords. This not only saves you time but also ensures a consistent naming convention across your files. Feel free to modify the data.txt file with your own set of keywords to customize the script according to your needs.
With the power of Python at your fingertips, cleaning up file names has never been simpler!
Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: