参数传递sys.argv, 文件递归搜索os.walk

这篇博客介绍了如何在Python中使用sys.argv传递命令行参数,并详细讲解了如何利用os.walk进行文件递归搜索。文章提醒在处理含有特殊字符的参数时需要注意转义,并给出了相关代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用sys.argv可以传递参数
sys.argv[0]表示代码本身文件路径
所以传入的文件从sys.argv[1], 开始算起,传入内容大概是以空格进行分割

有时传入的内容中,有特殊字符,注意用反斜线转义

输入命令,如下

    find_case.py '\\CLK38M_PROC\[115\].U_CLK38M_2FF_SYNC.INST_COMMON_SYNC_SVA_CHK .ipdb_common_sync_glitch_chk'

或者

phthon change.py /home/eagle2.work/regression_simfab/log

code如下

import sys
root=sys.argv[1]

或者,全代码


#!/usr/bin/env python

import os
import re
import sys

checker_log =sys.argv[1]
root='/home/eagle2.work3/regression_simfab/XG756_C0/XG756_C0_SPEC_3_3_SOC_TB_sva_case_search'
search_argu=[['check_logfiles.log.bz2'],['sf_tc.store','check_logfiles.log'],['<result>FAILED','<item>check_logs failed']]

def detect():#ergodic the files in path root                            #search_argu[1] is the two search search_arguments in file2
    i=0
    global check_file1
    global check_file2
    global rt
    for rt,dirs,files in os.walk(root):#ergodic the files in path root
        for file in files:
            i+=1
            #print(i)
            #detect zip file
            # print(rt)
            path=rt+'/'
            pathlist=os.listdir(path)
            for filename in pathlist:
                if filename.endswith(str(search_argu[0][0])):
                 #   print("*******need to bunzip first********")
                    order1='cd '+path+' && bunzip2 '+filename
                 #   print(rt)
                    os.system(order1)
                else:
                    pass
            if (pathlist.count(search_argu[1][0])==0) or (pathlist.count(search_argu[1][1])==0):
                i+=1
              #  print('file not found')
              #  print(i)
                break
            try:
                check_file1=path+search_argu[1][0]
                check_file2=path+search_argu[1][1]
            except FileNotFoundError:
             #   print('can not find files')
                sys.exit()
            check2(check_file2)
          #  print ("##################################################")
            break
def check1(file):#check file st_tc.store has [<result>FAILED] [<item>check_logs failed] or not
    a=0
    b=0
    #print('check1')
    open_file1=open(file)
    #print(open_file1)
    for line in open_file1:
        if(re.search(search_argu[2][0],line)):
            a=1
            break
    for line in open_file1:
        if (re.search(search_argu[2][1],line)):
            b=1
            break
    open_file1.close()
    if(a+b==2):
      #  print('check1 succeed')
        return(1)
    else:
       # print('check1 failed')
        return(0)
def check2(file):#check file check_logfiles.log
    num=0
    should_detect=[]
    remain_file=[]
    open_file2=open(file)
    if(check1(check_file1)==1):
        for line in open_file2:
            num+=1
            if(re.search(checker_log,line)!=None):
                should_detect.append(line)
            else:
                pass
        if len(should_detect) > 0:
          # print(len(should_detect))
           print(rt)
           print ('%d times found in this case' %(len(should_detect)))
        else:
         #  print ("can't find in this case")
           pass
        open_file2.close()

if __name__=='__main__':
    detect()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值