toolkit / utils /remove_tag.py
k4d3's picture
renames
12d27fb
raw
history blame
1.04 kB
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# ใƒ•ใ‚กใ‚คใƒซใ‹ใ‚‰ๆŒ‡ๅฎšใ•ใ‚ŒใŸๅ˜่ชžใ‚’ๅ‰Š้™คใ™ใ‚‹
# Remove a specified word from a file
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)