本文實例為大家分享了python實現遞歸查找某個路徑下所有文件中的中文字符,供大家參考,具體內容如下
# -*- coding: utf-8 -*- # @ description: # @ author: # @ created: 2018/7/21 import re import sys import os reload(sys) sys.setdefaultencoding("utf8") def translate(str): out = set() line = str.strip().decode('utf-8', 'ignore') # 處理前進行相關的處理,包括轉換成Unicode等 p2 = re.compile(ur'[^\u4e00-\u9fa5]') # 中文的編碼范圍是:\u4e00到\u9fa5 zh = " ".join(p2.split(line)).strip() # zh = "\n".join(zh.split()) #dsds經過相關處理后得到中文的文本 for s in zh.split(): out.add(s) # 經過相關處理后得到中文的文本 return out def extract_file(path): result = set() try: f = open(path) # 打開文件 lines = f.readlines() for line in lines: string = translate(line) if string: result.update(string) except Exception as e: pass return result def extract(path): result = set() files = os.listdir(path) for file in files: if not file.startswith("."): if not os.path.isdir(path + "/" + file): # 判斷是否是文件夾,不是文件夾才打開ssgsg判斷是否是文件夾,不是文件夾才打開 sub_file = extract_file(path + "/" + file) if sub_file: result.update(sub_file) else: print file child = extract(path + "/" + file) if child: result.update(child) return result if __name__ == '__main__': path = "/Users/common" result = extract(path) res_file = open("result.txt", "w") for s in result: res_file.write(s + "\n")
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
