简体   繁体   English

python:[try +除外]如何确保try下的所有命令都已执行?

[英]python: [try + except] How to make sure all command under try is executed?

This is a very beginners' question. 这是一个非常初学者的问题。 But I cannot find answer... 但是我找不到答案...

I want to run ALL lines under try . 我想在try下运行所有行。 If ANY lines are unexecutable, run except . 如果ANY行不可执行,则运行except

For example, I have some codes like this... 例如,我有一些这样的代码...

for i in range(0,100):
    try:
        print "a"
        print "b"
        print i, C
        print "d"
    except:
        print i, "fail"

I want to have ALL the lines under try executed. 我想try执行所有行。 In this example, print C is unexecutable, because C is not pre-defined. 在此示例中, print C C是不可执行的,因为C不是预定义的。

Thus, I want to ONLY run print i, "fail" under except , because NOT ALL lines under try are executable. 因此,我想运行print i, "fail"except ,因为NOT ALL下线try是可执行的。

To be more clearly, The code above gives me the result: 更清楚地说,上面的代码给了我结果:

a
b
0 0 Fail
a
b
1 1 Fail
a
b
2 2 Fail
...

But I want to have a result like this: 但我想要这样的结果:

0 Fail
1 Fail
2 Fail
...

How can I do this? 我怎样才能做到这一点? Thanks. 谢谢。

UPDATE: 更新:
To be more clearly, I am not asking for a result, I am asking for a method. 更清楚地说,我不是在寻求结果,而是在寻求一种方法。 I think there should be some commands to control try . 我认为应该有一些命令来控制try

You could alter the code so that it only prints when all expressions are valid: 您可以更改代码,以便仅在所有表达式均有效时才打印:

for i in range(0,3):
    try:
        print ("a" + "\n" +
               "b" + "\n" + 
               C   + "\n" +
               "d")
    except:
        print (i, "fail")

Another variation to the same principle uses a list to store the things to be printed if all is successful: 相同原则的另一种变体是使用一个列表来存储所有成功的要打印的东西:

for i in range(0,3):
    try:
        collect = []
        collect.append("a")
        collect.append("b")
        collect.append(C)
        collect.append("d")
        print("\n".join(collect))
    except:
        print (i, "fail")

You could temporarily redirect stdout and only print in the except: 您可以临时重定向标准输出,仅在以下位置打印:

import sys
from StringIO import StringIO


for i in range(0,100):
    sys.stdout = StringIO()
    try:
        print "a"
        print "b"
        print i, C
        print "d"
    except Exception as e:
        sys.stdout = sys.__stdout__
        print i, "fail" # or print e
sys.stdout = sys.__stdout__

Which would give you: 这会给你:

0 fail
1 fail
2 fail
3 fail
4 fail
5 fail
6 fail
7 fail
8 fail
9 fail
10 fail
11 fail
12 fail
13 fail
14 fail
15 fail
16 fail
17 fail
18 fail
19 fail
20 fail
21 fail
22 fail
23 fail
24 fail
25 fail
26 fail
27 fail
28 fail
29 fail
30 fail
31 fail
32 fail
33 fail
34 fail
35 fail
36 fail
37 fail
38 fail
39 fail
40 fail
41 fail
42 fail
43 fail
44 fail
45 fail
46 fail
47 fail
48 fail
49 fail
50 fail
51 fail
52 fail
53 fail
54 fail
55 fail
56 fail
57 fail
58 fail
59 fail
60 fail
61 fail
62 fail
63 fail
64 fail
65 fail
66 fail
67 fail
68 fail
69 fail
70 fail
71 fail
72 fail
73 fail
74 fail
75 fail
76 fail
77 fail
78 fail
79 fail
80 fail
81 fail
82 fail
83 fail
84 fail
85 fail
86 fail
87 fail
88 fail
89 fail
90 fail
91 fail
92 fail
93 fail
94 fail
95 fail
96 fail
97 fail
98 fail
99 fail

But there are no doubt much better ways to do whatever it is you are trying to do in your real logic. 但是,毫无疑问,有更多更好的方法可以按照您的实际逻辑去做。

Bar seeing into the future you cannot print in real time if all calls are going to be successful as you have not seen them all so you would need to store the output and see if there were any errors. 将来,您将无法实时打印是否所有呼叫都将成功,因为您尚未看到所有呼叫,因此您需要存储输出并查看是否有任何错误。

import sys
from StringIO import StringIO

stdo = StringIO()
errs = False
for i in range(0, 100):
    sys.stdout = stdo
    try:
        print "a"
        print "b"
        print i
        print "d"
    except Exception as e:
        sys.stdout = sys.__stdout__
        errs = True
        print i, "fail"
        sys.stdout = stdo

sys.stdout = sys.__stdout__
if not errs:
    print(stdo.getvalue())

Any error will show in real time but you will have to wait until the end to see if all are successful. 任何错误都会实时显示,但是您必须等到最后,看看一切是否成功。

This is very similar to what @trincot posted, not sure if it would meet your requirements. 这与@trincot发布的内容非常相似,不确定是否满足您的要求。

Basically, move all of your prints to a separate function and call that in your try. 基本上,将所有打印件移至一个单独的函数,然后尝试进行调用。

def printer(i=None, C=None):
    print "a"
    print "b"
    print i, C
    print "d"    

for i in range(0,100):
    try:
        printer(i, C)
    except:
        print i, "fail"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM