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

时间:2012-03-05 23:52:34

标签: python quit

如何在条件句子运行后以编程方式停止python脚本。在下面的伪脚本中:

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") 

谢谢, 麦克

4 个答案:

答案 0 :(得分:2)

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()

答案 1 :(得分:1)

不确定我是否理解正确

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()

答案 2 :(得分:1)

我会这样做:

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()

any

的文档

答案 3 :(得分:1)

另一种方法:

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()