C51字符串提取python脚本

提取目录下所有c文件包含关键字的行的python脚本)

介绍

提取目录下所有子文件夹下c文件包含对应代码的行。写这个的主要用途是提取C51的字符串定义,默认提取“static code unsigned char *”字符所在行,用于多语言翻译时整理待翻译提示符。
示例代码:static code unsigned char *langZn={“中文”};

代码分享

import os,re
class SearchFile(object):
 def __init__(self,path='.'):
  self._path=path
  self.abspath=os.path.abspath(self._path) # 默认当前目录
     
 def findFunc(self,funcName,root):
  filelist=[]
  for root,dirs,files in os.walk(root):
   for name in files:
    fileName=re.match(".*\.c$",os.path.join(root, name))
    if fileName:
     fitfile=filelist.append(os.path.join(root, name))
  print('...........................................')
  for filepath in filelist:  
   if os.path.isfile(filepath):
    #打开文件查找是否存在对应函数定义
    fp = open(filepath, 'r')
    try:
     lines = fp.readlines()
    except:
     print("打开文件"+filepath+"异常")
    else:
     k = 0
     for strtemp in lines:
      out = re.match(".*"+funcName+".*",strtemp)
      if out :
       print(strtemp)
    
 def __call__(self):
  while True:
   workpath=input('查找当前目录? Y/N:')
   if(workpath == ''):
    break
   if workpath=='y' or workpath=='Y':
    root=self.abspath # 把当前工作目录作为工作目录
    print('当前工作目录:',root)
    #dirlist=os.listdir() # 列出工作目录下的文件和目录
    #print(dirlist)
   else:
    root=input('输入查找目录:')
    print('当前工作目录:',root)
   keyword=input('需要查找的函数名称:')
   if(keyword==''): 
     keyword="static code unsigned char *"
   self.findFunc(keyword,root) # 查找带指定字符的文件
   
if __name__ == '__main__':
 search = SearchFile()
 search()

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值