打标后的文件修改(删除括号及数字)

import os
import re

def remove_characters_from_file(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        content = file.read()
    
    # 删除所有中英文括号及数字
    new_content = re.sub(r'[\(\)\[\]\{\}()【】0-9]', '', content)
    
    with open(file_path, 'w', encoding='utf-8') as file:
        file.write(new_content)

def process_directory(directory):
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.endswith('.txt'):
                file_path = os.path.join(root, file)
                remove_characters_from_file(file_path)
                print(f'Processed: {file_path}')

if __name__ == "__main__":
    directory = input("请输入文件夹路径: ")
    process_directory(directory)
    print("处理完成。")

 

posted @ 2024-02-27 22:49  不上火星不改名  阅读(46)  评论(0)    收藏  举报