简体   繁体   中英

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM