简体   繁体   English

运行条件语句后以编程方式停止执行python脚本

[英]Programmatically stop execution of python script after running condition statement

How do you programmatically stop a python script after a condition sentence has run through. 条件语句执行完后,如何以编程方式停止python脚本。 In the pseudo script below: 在下面的伪脚本中:

for row in rows:

    if row.FIRSTDATE == row.SECONDDATE:
        pass
    else:
        print "FIRSTDATE does not match SECONDDATE " + row.UNIQUEID

## If I set my quit sequence at the this tab level, it quits after the first
## unmatched record is found. I don't want that, I want it to quit after all the
## unmatched records have been found, if any. if all records match, I want the
## script to continue and not quit

        sys.quit("Ending Script") 

Thanks, Mike 谢谢,迈克

quit_flag = False
for row in rows:

    if row.FIRSTDATE == row.SECONDDATE:
        pass
    else:
        print "FIRSTDATE does not match SECONDDATE " + row.UNIQUEID
        quit_flag = True

if quit_flag:
    print "Ending Script"
    sys.exit()

Another approach: 另一种方法:

mis_match = []

for row in rows:
    if row.FIRSTDATE != row.SECONDDATE:
        mis_match.append(row.UNIQUEID)

if mis_match:
  print "The following rows didn't match" + '\n'.join(mis_match)
  sys.exit()

not sure if i understand correctly 不知道我是否正确理解

doQuit = 0
for row in rows:
    if row.FIRSTDATE != row.SECONDDATE:
        print "FIRSTDATE does not match SECONDDATE " + row.UNIQUEID
        doQuit = 1
if doQuit: sys.exit()

I would do it like this: 我会这样做:

def DifferentDates(row):
    if row.FIRSTDATE != row.SECONDDATE:
        print "FIRSTDATE does not match SECONDDATE " + row.UNIQUEID
        return True
    else:
        return False

# Fill a list with Trues and Falses, using the check above
checked_rows = map(DifferentDates, rows)

# If any one row is different, sys exit
if any(checked_rows):
    sys.exit()

Documentation for any 任何文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:异常停止后,如何继续在try语句内执行 - Python : How to continue execution inside a try statement after an exception stop it 如何以编程方式停止执行python脚本,而不关闭python和维护变量? - How do I programmatically stop execution of a python script, without closing python and maintaining the variables? Python并发ThreadPoolExecutor - 如果满足条件则停止执行 - Python Concurrency ThreadPoolExecutor - stop execution if condition is met 在给我的循环停止条件后,它会继续。 打印语句有效,但代码继续运行 - after giving a condition for my loop to stop it continues. the print statement works, but the code continues running 在满足条件后停止检查语句 - stop checking if statement after condition is met 有没有办法在运行 if 语句以捕获 Python 中的异常后继续我的脚本? - Is there a way to continue my script after running an if statement to catch anomaly in Python? 停止通过用户命令执行python脚本? - stop execution of python script by user command? 根据条件执行 python 脚本成功/失败 - Execution of python script success/fail based on condition 如果python中的某些条件失败,如何停止执行累积函数 - How to stop execution of cumulative functions if some condition fails in python 当父Bash shell脚本被终止时停止执行python脚本 - Stop execution of python script when parent Bash shell script is killed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM