首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用异常处理防止系统崩溃时出现的几行

使用异常处理防止系统崩溃时出现的几行
EN

Stack Overflow用户
提问于 2020-04-29 17:37:52
回答 1查看 13关注 0票数 0

我正在尝试使用异常处理来防止我的系统崩溃,我使用了下面的代码,而这个csv文件实际上并不存在。但是这个系统一直在给我答案。谁能帮我纠正我的代码please....help我

以下是我的源代码:

代码语言:javascript
运行
复制
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))

结果:

代码语言:javascript
运行
复制
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''
..................
EN

回答 1

Stack Overflow用户

发布于 2020-04-29 17:43:48

捕获预期意味着您的系统不会以适当的错误代码退出。相反,它执行您放在异常行中的任何操作,并继续执行您的代码。从docs

如果在执行try子句期间发生异常,则跳过子句的其余部分。然后,如果它的类型与以except关键字命名的异常匹配,则执行except子句,然后在try语句之后继续执行。

这就是为什么您从不离开while循环的原因。这是对循环的快速修复:

代码语言:javascript
运行
复制
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时要执行的操作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61498705

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档