import subprocess def counter(start=1): while True: yield start start += 1 counter = counter() while True: count = next(counter) cmd = ['pcbuild/python', 'lib/test/regrtest.py', '-v', 'test_mailbox'] res = subprocess.Popen(cmd).wait() if res != 0: break print('ran {count} times, then failed with {res}'.format(**vars()))