Pthon遍历文件目录,去除文件中的中文

有需要,将代码目录中的所有文件,去除代码文件中的中文。当然有些格式的中文无法去除

import os, re
root_folder = "E:/OJK_review/h5" ---"""按照这个格式输入自己想遍历的地址"""
file_list=[]
for foldername, subfolders, filenames in os.walk(root_folder):
    for filename in filenames:
        file_path = os.path.join(foldername, filename)
        #判断文件后缀是否,aar之类的无法处理
        if file_path.endswith(('.txt', '.vue', '.py', '.html', '.java','.js','.json')):
            with open(file_path, 'r', encoding='utf-8') as file:
                content = file.read()
            #去除中文
            content_without_chinese = re.sub('[\u4e00-\u9fa5]', '', content)
            with open(file_path, 'w', encoding='utf-8') as file:
                file.write(content_without_chinese)
                print(f"Removed Chinese characters from {file_path}")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值