File size: 939 Bytes
1b6e1a9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#!/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)
|