[Tutor] Vol 127, Issue 15
Marc Tompkins
marc.tompkins at gmail.com
Sat Sep 6 00:10:15 CEST 2014
On Fri, Sep 5, 2014 at 2:32 PM, Crush <crushed26 at gmail.com> wrote:
> Ok nevermind, I did not figure it out. My code...
>
> count = 0
> while count < 3:
> count += 1
> Subprocess.Popen('command')
> if count == 3:
> sys.exit()
>
> This does not work as I want it to; it consecutively executes the command three times in a row. I only want it to execute once. However, if there is an error, I want it to try again, but only if count does not equal 3. If count equals 3, i want it to give up and exit or do something else.
I don't know what "command" does, or what an "error" would mean for
your purposes - if you want an exact answer, you'll need to provide
more detail. However, I'm going to assume that if an "error" occurs,
a Python exception of some sort gets raised. If so, here's how I
would handle this:
count = 0
success = False
while (count < 3) and not success:
try:
Subprocess.Popen('command')
success = True
except: # (you shouldn't use a bare except, but I don't know what
to expect)
count +=1
sys.exit()
More information about the Tutor
mailing list