Created
October 9, 2025 03:26
-
-
Save YesThatAllen/9ff75735a985955a7d6608c23dd2e108 to your computer and use it in GitHub Desktop.
For all eml in a folder, append an identifier to the subject line (if it doesn't already exist) and add a subject line if it was missing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import os | |
| import sys | |
| def process_eml(file_path, subj_prefix): | |
| try: | |
| with open(file_path, 'r', encoding='utf-8', errors='ignore') as f: | |
| lines = f.readlines() | |
| modified = False | |
| subject_found = False | |
| for i, line in enumerate(lines): | |
| if line.lower().startswith("subject:"): | |
| subject_found = True | |
| subject_line = line.strip() | |
| # Add prefix only if not already present | |
| if not subject_line.startswith(f"Subject: {subj_prefix}"): | |
| new_subject = f"Subject: {subj_prefix}{subject_line[len('Subject: '):]}" | |
| lines[i] = new_subject + "\n" | |
| modified = True | |
| break # only process the first Subject line | |
| if not subject_found: | |
| # Insert new Subject line only once | |
| new_subject_line = f"Subject: {subj_prefix}\n" | |
| # Optional: insert before the first blank line or header boundary | |
| insert_index = 0 | |
| for j, line in enumerate(lines): | |
| if line.strip() == "": | |
| insert_index = j | |
| break | |
| lines.insert(insert_index, new_subject_line) | |
| modified = True | |
| if modified: | |
| with open(file_path, 'w', encoding='utf-8') as f: | |
| f.writelines(lines) | |
| print(f"Updated: {file_path}") | |
| else: | |
| print(f"No change: {file_path}") | |
| except Exception as e: | |
| print(f"Error processing {file_path}: {e}") | |
| def main(): | |
| if len(sys.argv) < 3: | |
| print("Usage: add_prefix_to_eml_subjects.py <folder_path> <subj_prefix>") | |
| sys.exit(1) | |
| folder = sys.argv[1] | |
| subj_prefix = sys.argv[2] | |
| for root, _, files in os.walk(folder): | |
| for file in files: | |
| if file.lower().endswith(".eml"): | |
| process_eml(os.path.join(root, file), subj_prefix) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
% python3 add_prefix_to_eml_subjects.py path/to/folder/of/eml "[prefix] "(Note the trailing space after the closing bracket
])