码迷,mamicode.com
首页 > 编程语言 > 详细

python读取文件夹及其子文件夹下所有含有中文字符串的lua文件

时间:2015-02-13 18:19:37      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:

#!/usr/bin/python #coding=utf-8

import sys import os import shutil import struct import hashlib import re

 

G_WORK_PATH = "E:\phoneclient" G_TARGET_PATH = [  #lua:  ["%s/sdz/script",     "(.*\.lua)",], ] G_OUTPUT_LIST = []

 

#跳转至当前目录 os.chdir(os.path.abspath(sys.path[0])) os.environ[‘LANG‘] = "zh_CN.utf8"

 

#检查中文内容 def Check():  global G_WORK_PATH  global G_TARGET_PATH

 for target_path in G_TARGET_PATH:   final_path = target_path[0]%G_WORK_PATH

  match_str = ""   if len(target_path) > 1:    match_str = target_path[1]

  #判断是文件还是文件夹   if os.path.isfile(final_path):    CheckSingleFile(final_path)    continue   else:    final_path = CheckEndWithOsSep(final_path)

  for dir_info in os.walk(final_path):    #跳过隐藏文件夹    if -1 != dir_info[0].find("/.") or -1 != dir_info[0].find("\\."):     continue

   tmp_path = dir_info[0][len(final_path):]    if "" != tmp_path:     tmp_path = CheckEndWithOsSep(tmp_path)

   for i in dir_info[2]:     #这几个文件是ANSI编码,也没有中文,暂时屏蔽     if "reslvlogicdumper.lua" == i or "protocalonlinegift.lua" == i or "equipexchangelayer.lua" == i or "facechatexlayer.lua" == i:      continue

    if "" != match_str:      match_ret = re.match(match_str, "%s%s"%(tmp_path, i))      if match_ret and len(match_ret.groups()) > 0:       CheckSingleFile("%s%s%s"%(final_path, tmp_path, i))     else:      CheckSingleFile("%s%s%s"%(final_path, tmp_path, i))

 

#检查单个文件 def CheckSingleFile(file_path):  global G_OUTPUT_LIST

 tmp_file = open(file_path, "rb+")  file_info = tmp_file.readlines()  tmp_file.close()

 found = False  line_count = 0  for str_line in file_info:   line_count += 1   if CheckSingleLine(OnlyStr(str_line)):    if not found:     found = True     G_OUTPUT_LIST.append(file_path + "\n")    #记录文件名和行数    G_OUTPUT_LIST.append(str(line_count) + "\n")

 

#检查单独一行 def CheckSingleLine(str_line):  if "--" == str_line[:2]:   return False

 zh_match = re.match(ur".*[\‘\"].*[\u4e00-\u9fa5]+.*[\‘\"].*", str_line.decode("utf8"))  #排除输出日志内容  log_match = re.match(ur".*(?:PrintLog|colog|cmd_colog).*[\‘\"].*[\u4e00-\u9fa5]+.*[\‘\"].*", str_line.decode("utf8"))  return zh_match and not log_match

 

#补充斜杠 def CheckEndWithOsSep(path_str):  if path_str.endswith("/") or path_str.endswith("\\"):   return path_str  return path_str + os.path.sep

 

#剔除空白字符 def OnlyStr(tmp_str):  return tmp_str.strip("\r\n").strip("\n").strip("\t").strip()

 

#输出结果 def WriteResult():  global G_OUTPUT_LIST

 tmp_file = open("check.txt", "w")  tmp_file.writelines(G_OUTPUT_LIST)  tmp_file.close()

 

if __name__ == "__main__":  Check()  WriteResult()  

python读取文件夹及其子文件夹下所有含有中文字符串的lua文件

标签:

原文地址:http://www.cnblogs.com/yyxt/p/4290649.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!