несколько строк, появляющихся при использовании обработки исключений, чтобы предотвратить сбой системы - PullRequest
0 голосов
/ 29 апреля 2020

Я пытаюсь использовать обработку исключений для предотвращения сбоя моей системы, я использовал приведенные ниже коды, где этот файл 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''
..................

1 Ответ

0 голосов
/ 29 апреля 2020

Отлов ожидания означает, что ваша система не выходит с соответствующим кодом ошибки. Вместо этого он делает все, что вы положили в строку исключения, и продолжает выполнение вашего кода. Из документов :

Если во время выполнения предложения try возникает исключение, остальная часть предложения пропускается. Затем, если его тип соответствует исключению, названному в честь ключевого слова exc, выполняется условие exc, а затем выполнение продолжается после оператора try.

Вот почему вы никогда не оставляете while l oop. Это быстрое исправление цикла:

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 не найден.

...