当使用异常处理来防止系统崩溃时,出现了几行字。

问题描述 投票:0回答:1

我试图使用异常处理来防止系统崩溃,我使用了下面的代码,这个csv文件实际上并不存在。但系统是不断给我答案。谁能帮我改一下我的代码好吗......帮帮我吧,拜托了

这是我的源代码。

while True:
    try:
        import pandas as pd  # data processing, csv file I/O(e.g pd.read_csv)

        df = pd.read_csv('C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv')
        print(df)
    except Exception as err:
            print("Uh oh, please send me this message: '{}'" .format(err))

结果:

Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
Uh oh, please send me this message: '[Errno 2] File b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv' does not exist: b'C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv''
..................
python python-3.x exception error-handling
1个回答
0
投票

捕获一个期望意味着你的系统不会以适当的错误代码退出。相反,它做的是你在异常行中输入的任何内容,并继续执行你的代码。从 文件:

如果在执行try子句的过程中发生异常,则跳过该子句的其他部分。然后,如果它的类型与except关键字后的异常相匹配,except子句就会被执行,然后在try语句之后继续执行。

这就是为什么你永远不要离开while循环的原因。这是对循环的快速修复。

while True:
    try:
        import pandas as pd  # data processing, csv file I/O(e.g pd.read_csv)

        df = pd.read_csv('C:/Users/User/Desktop/Coding/parsehubjsonfileeg/assd.csv')
        print(df)
    except Exception as err:
        print("Uh oh, please send me this message: '{}'" .format(err))
        break

进一步的调整取决于当你的csv没有找到时你想做什么。

© www.soinside.com 2019 - 2024. All rights reserved.