Running multiple scripts in sequence in Python.

huangapple go评论49阅读模式
英文:

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&quot;C:00 nodes\{i}00_beta_0.01_50.0_1.0ND_3.py&quot;).read())

The error encountered while running script in folder 2 is

File &quot;&lt;string&gt;&quot;, line 618, in &lt;module&gt;

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&quot;C:00 nodes\{i}00_beta_0.01_50.0_1.0ND_3.py&quot;).read())
    except Error as e:
        print(e)

huangapple
  • 本文由 发表于 2023年2月6日 21:00:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361638.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定