-1

在这里,我正在使用“for”循环读取和比较两个日志中的值。问题是我无法在 sys.exit 命令之后继续到下一个 TC。让我知道是否需要更多说明

f = open('/tmp/ftplog', 'r')
    for line in f:
        m = re.findall("5\d+", line)
        #print m
        fd = open('/tmp/tellog', 'r')
        for line in fd:
            n = re.findall(r"5\d+", line)
            #print n
            if  m == n:                 
             print "passed"

            sys.exit()  

####TC-02####

def tc02(ipadrr,login,password,ftpipaddr,ftplogin,ftppassword,ftpfilename):

    try:    

                telconn2 = pexpect.spawn(ipadrr)
4

4 回答 4

4

您可以添加将在退出时使用的钩子atexithttp://docs.python.org/2/library/atexit.html?highlight=atexit#atexit

但是,需要在一个简单的脚本中执行此操作通常表明您的逻辑是错误的。你真的需要退出吗?你可以抛出一个异常吗?break? return? 例如,尝试在函数中包含逻辑,函数return在完成时生成,以及一些调用它并对返回的结果执行某些操作的代码。

于 2013-05-21T05:34:36.170 回答
4

sys.exit 实际上会抛出一个SystemExit异常,你可以捕获它,但你真的不应该. 重组你的程序,这样你就不必了。

于 2013-05-21T05:36:59.697 回答
2

尝试这个

print "passed"

return ####instead of sys.exit use return
于 2013-05-21T06:23:49.480 回答
1

A way to do this if it's really needed is to call the same script but with different parameters in a subprocess just before you exit it like this:

import subprocess
p = subprocess.Popen("exec your_script.py -parameters parameter", stdout=subprocess.PIPE, shell=True)

Then you can add some checks for a specific parameter that you will provide and execute only this part of your code (e.g. the tc02() function that you need).

Just keep in mind that once you call the script as a subprocess, you won't be able to stop it from the console with Ctr+C, since this will kill the parent process, but not the child processes. In order to kill everything you need to call a method like this:

p.kill()
于 2018-06-13T14:14:46.957 回答