[英]Python how to rerun code after it stops
i am newbie to Programming,i am running this python code in my computer Normally it runs correctly, but sometimes the code stops due to network conditions. 我是编程的新手,我在计算机上运行此python代码。正常情况下,它可以正常运行,但有时由于网络条件而停止运行。 once it stopped i have to run the code manually.
一旦停止,我必须手动运行代码。 Does anyone know how to modify this code to retry if the code fails to run
是否有人知道如何修改此代码以在代码无法运行时重试
This is the code 这是代码
for line in f:
api.update_status(line)
time.sleep(1200)
The code in Noel's answer makes it more likely that the program doesn't stop until the loop goes through every item in f
. Noel的答案中的代码使程序更有可能直到循环遍历
f
每个项目才停止。
The problem though is that an exception could occur in the line after except
and the program would stop again. 但是,问题在于,
except
之后的行中可能会发生异常,程序将再次停止。
try this: 尝试这个:
i = 0
while i < len(f):
try:
api.update_status(f[i])
i += 1
except:
pass
This way, i
only gets incremented if api.update
is successful so no items will be skipped and the program wont stop until all f
are update
d 这样,如果
api.update
成功, i
只会递增,因此不会跳过任何项目,并且直到所有f
被update
d为止,程序都不会停止
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.