toolkit / remove_tag
k4d3's picture
remove_tag
1b6e1a9
raw
history blame
939 Bytes
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pathlib
import re
def remove_word_from_file(file_path, word):
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
# Remove the word with the comma and space if there is one after it
pattern = re.compile(r'\b' + re.escape(word) + r',?\s?')
new_content = pattern.sub('', content)
with open(file_path, 'w', encoding='utf-8') as file:
file.write(new_content)
def remove_word_from_directory(directory, word):
path = pathlib.Path(directory)
for txt_file in path.rglob('*.txt'):
remove_word_from_file(txt_file, word)
if __name__ == "__main__":
import sys
if len(sys.argv) != 3:
print("Usage: python script.py <directory> <word>")
sys.exit(1)
target_directory = sys.argv[1]
target_word = sys.argv[2]
remove_word_from_directory(target_directory, target_word)