Sam
Sam

Reputation: 227

Try / Except in Python to show which file threw the error

I am writing a py to import in a large amount of files, manipulate them and then output to .csv. Which is cake in Pandas, however I have no control over the files coming in so I am trying to write the script to have an exception on how to handle if files come in the "wrong" way.

Anyway, I am using a Try/Except to show the user that there is a KeyError in one of the files (basicially there is a " in a cell when the datatype is int).

My question is: Is there a way to have the except: bring back the file name of the file that caused the error??

for csv in csvList:
        df = pd.read_csv(csv, header=0, skip_blank_lines=True, skipinitialspace=True)\
            .dropna(how='all')

        try:
            df[0] = df[0].astype(int)
            df[1] = df[1].astype(int)
            df[2] = df[2].astype(int)
            df[3] = df[3].astype(int)

            report_path = 'UPC_Ready_for_Import'
            if not os.path.exists(report_path):
                os.makedirs(report_path)

            df.to_csv(os.path.join(report_path, csv + '_import.csv'), index=False)

        except KeyError:
            print('Error within file, please review files')

Upvotes: 0

Views: 688

Answers (2)

running.t
running.t

Reputation: 5709

Assuming csvList contains list of input file paths:

for csv in csvList:
    ....
    try:
        ...
    except KeyError:
         print('Error within file {}, please review files'.format(csv))

Upvotes: 4

mgross
mgross

Reputation: 652

You could write, something like this, I guess:

for csv in csvList:
        df = pd.read_csv(csv, header=0, skip_blank_lines=True, skipinitialspace=True)\
            .dropna(how='all')

        try:
            df[0] = df[0].astype(int)
            df[1] = df[1].astype(int)
            df[2] = df[2].astype(int)
            df[3] = df[3].astype(int)

            report_path = 'UPC_Ready_for_Import'
            if not os.path.exists(report_path):
                os.makedirs(report_path)

            file_name = os.path.join(report_path, csv + '_import.csv')
            df.to_csv(file_name, index=False)

        except KeyError:
            print('Error within file', file_name ', please review files')

The main idea is to store the file name in a variable file_name and use it in the except block.

Upvotes: 0

Related Questions