Have you ever downloaded subtitle files (.srt) for a TV series, only to find that every single file contains an annoying advertisement or credit, like "Subtitles by Subz.com"? Manually opening and editing hundreds of files is a tedious, soul-crushing task. This is a perfect job for automation!

In this tutorial, we'll create a powerful yet simple Python script that can scan an entire folder (including all its subfolders), find every `.srt` file, and remove any specific sentences you want. It's a practical example of how a few lines of Python can save you hours of manual work.

Prerequisites

All you need is Python 3 installed on your computer. We will only be using the built-in `os` module, so no external libraries are required.

The Complete Python Script

Here is the full script. You can save this code in a file named `srt_cleaner.py` and run it directly.

import os

def delete_sentences_in_srt_files():
    """
    Looks through all .srt files in a specified folder (and its subfolders),
    deletes certain sentences, and saves the updated files in an "updated" subfolder.
    """

    source_directory = input("Enter the path to the folder containing the .srt files: ")

    # Validate if the source directory exists
    if not os.path.isdir(source_directory):
        print(f"Error: Directory '{source_directory}' not found.")
        return

    sentences_to_delete_input = input(
        "Enter the sentences to delete (separate multiple sentences with a semicolon ';'): "
    )
    sentences_to_delete = [s.strip() for s in sentences_to_delete_input.split(';') if s.strip()]

    if not sentences_to_delete:
        print("No sentences to delete were provided. Exiting.")
        return

    print(f"\nSentences to delete: {sentences_to_delete}")
    print("\nStarting file processing...")

    updated_files_count = 0

    for root, _, files in os.walk(source_directory):
        for filename in files:
            if filename.lower().endswith(".srt"):
                filepath = os.path.join(root, filename)
                print(f"Processing: {filepath}")

                try:
                    with open(filepath, 'r', encoding='utf-8') as f:
                        content = f.read()

                    original_content = content # Keep original to check if changes were made

                    for sentence in sentences_to_delete:
                        content = content.replace(sentence, '') # Delete the sentence

                    if content != original_content:
                        # Create the 'updated' subfolder
                        relative_path = os.path.relpath(root, source_directory)
                        updated_subfolder_path = os.path.join(source_directory, "updated", relative_path)
                        os.makedirs(updated_subfolder_path, exist_ok=True)

                        output_filepath = os.path.join(updated_subfolder_path, filename)

                        with open(output_filepath, 'w', encoding='utf-8') as f:
                            f.write(content)
                        print(f"  -> Updated and saved to: {output_filepath}")
                        updated_files_count += 1
                    else:
                        print("  -> No changes needed.")

                except Exception as e:
                    print(f"  -> Error processing {filepath}: {e}")

    print(f"\nProcessing complete. {updated_files_count} files were updated.")
    print(f"Updated files are saved in the 'updated' subfolder within '{source_directory}'.")

if __name__ == "__main__":
    delete_sentences_in_srt_files()

Step-by-Step Code Breakdown

Let's walk through how this script works, piece by piece.

1. Getting User Input

The script starts by asking the user for two things: the folder to search and the sentences to delete. This makes it flexible and reusable.

source_directory = input(...)
sentences_to_delete_input = input(...)

We then process the sentences input, splitting it by the semicolon (`;`) character. This allows you to remove multiple different sentences in one go. The `strip()` method removes any accidental leading/trailing whitespace.

sentences_to_delete = [s.strip() for s in sentences_to_delete_input.split(';') if s.strip()]

2. Walking Through the Directory Tree

This is the core of the file discovery process. `os.walk(source_directory)` is a powerful generator that "walks" through a directory tree, visiting the top folder and every single one of its subfolders.

for root, _, files in os.walk(source_directory):
    for filename in files:
        if filename.lower().endswith(".srt"):
            # We found an SRT file!
            ...

For each folder it visits, it gives us the `root` path, a list of directories, and a list of `files`. We loop through the files and check if they end with `.srt` (case-insensitively).

3. Reading, Replacing, and Checking for Changes

Once we find an `.srt` file, we open it, read its entire content into a string, and then perform the replacement.

with open(filepath, 'r', encoding='utf-8') as f:
    content = f.read()

original_content = content

for sentence in sentences_to_delete:
    content = content.replace(sentence, '')

if content != original_content:
    # Save the file because changes were made
    ...
Using `content.replace(sentence, '')` is a simple and effective way to delete the target text. We also keep a copy of the `original_content` to easily check if any changes were actually made. There's no point in saving a new file if nothing was removed.

4. Safely Saving the Updated File

This is a crucial feature for safety. Instead of overwriting your original files, the script creates a new folder named `updated` inside your source directory. It cleverly recreates the original subfolder structure within this `updated` folder, so you can easily find your modified files while keeping the originals intact.

relative_path = os.path.relpath(root, source_directory)
updated_subfolder_path = os.path.join(source_directory, "updated", relative_path)
os.makedirs(updated_subfolder_path, exist_ok=True)

output_filepath = os.path.join(updated_subfolder_path, filename)
with open(output_filepath, 'w', encoding='utf-8') as f:
    f.write(content)
  • os.path.relpath(...) gets the subfolder path relative to the main source directory (e.g., "Season 1/").
  • os.makedirs(..., exist_ok=True) creates the necessary folders without throwing an error if they already exist.

How to Run the Script

  1. Save the code above into a file named srt_cleaner.py.
  2. Open your terminal or command prompt.
  3. Navigate to the directory where you saved the file using the `cd` command.
  4. Run the script using the command: python srt_cleaner.py
  5. The script will then prompt you to enter the folder path and the sentences you wish to delete.

For example, your interaction might look like this:

Enter the path to the folder containing the .srt files: /Users/rohit/Movies/My-Show
Enter the sentences to delete (separate multiple sentences with a semicolon ';'): Subtitles downloaded from Subz.com; Support us by donating

Conclusion

You now have a powerful utility script that automates a tedious manual task. This example shows the real power of Python for batch processing and file manipulation. You can adapt this script for many other purposes, such as finding and replacing text in log files, cleaning up text documents, or any other task that involves processing many files at once. It's a fantastic tool to have in your developer toolkit.