英文:
Running multiple scripts in sequence in Python
问题
我根据列表 I
和以下可执行文件依次运行多个脚本。然而,如果文件夹中的某个脚本(比如 2
)出现错误,它会终止,而不会继续到文件夹 3
。基本上,我希望如果当前脚本出现错误,可执行文件会继续执行下一个脚本。如何实现这一点?
I=[1,2,3]
for i in I:
exec(open(rf"C:00 nodes\{i}00_beta_0.01_50.0_1.0ND_3.py").read())
在文件夹 2
中运行脚本时出现的错误是:
File "<string>", line 618, in <module>
ValueError: max() arg is an empty sequence
英文:
I am running multiple scripts in sequence according to the list I
and the following executable. However, when one of the script in folder (say, 2
) runs into an error, it terminates instead of moving to folder 3
. Basically I want the executable to move onto the next script if there is an error in the present script. How do I do this?
I=[1,2,3]
for i in I:
exec(open(rf"C:00 nodes\{i}00_beta_0.01_50.0_1.0ND_3.py").read())
The error encountered while running script in folder 2
is
File "<string>", line 618, in <module>
ValueError: max() arg is an empty sequence
答案1
得分: 0
你可以使用 try-except 块。
I = [1, 2, 3]
for i in I:
try:
exec(open(rf"C:00 nodes\{i}00_beta_0.01_50.0_1.0ND_3.py").read())
except Error as e:
print(e)
英文:
You could use a try-except block.
I=[1,2,3]
for i in I:
try:
exec(open(rf"C:00 nodes\{i}00_beta_0.01_50.0_1.0ND_3.py").read())
except Error as e:
print(e)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论