What is a good way to handle exceptions when trying to read a file in python?

Charles Holbrow :

I want to read a .csv file in python.

  • I don't know if the file exists.
  • My current solution is below. It feels sloppy to me because the two separate exception tests are awkwardly juxtaposed.

Is there a prettier way to do it?

import csv    
fName = "aFile.csv"

try:
    with open(fName, 'rb') as f:
        reader = csv.reader(f)
        for row in reader:
            pass #do stuff here
    
except IOError:
    print "Could not read file:", fName
jscs :

I guess I misunderstood what was being asked. Re-re-reading, it looks like Tim's answer is what you want. Let me just add this, however: if you want to catch an exception from open, then open has to be wrapped in a try. If the call to open is in the header of a with, then the with has to be in a try to catch the exception. There's no way around that.

So the answer is either: "Tim's way" or "No, you're doing it correctly.".


Previous unhelpful answer to which all the comments refer:

import os

if os.path.exists(fName):
   with open(fName, 'rb') as f:
       try:
           # do stuff
       except : # whatever reader errors you care about
           # handle error

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What is preferable way to handle exceptions?

What is the cleaner way to handle exceptions

What is the best way to handle exceptions in this scenario

What is a good way to prevent overflows when converting float to bigint in Python?

What is the correct way to handle exceptions in a REST API that produces an Excel or PDF file as response?

What is the problem with a txt file when trying to read this into R environment?

Is there a way to handle exceptions automatically with Python Click?

How to handle Python exceptions in a decent way?

Python: Correct way to handle chain exceptions

"Stale file handle" error, when process trying read the file, that other process already had deleted

How to handle Python exceptions when no action is desired

How to handle exceptions while appending a list in python with data read from a dict that stores data read from a .json file?

File not found when trying to read

What is a good way to get this structure in a JSON file

Recommended way to handle exceptions?

What is the proper way to load images in Flutter and handle exceptions on load

Completable futures. What's the best way to handle business "exceptions"?

What is the recommended way to handle (or suppress) exceptions thrown by an AutoCloseable?

Two exceptions "at the same time", what is the proper way to handle this situation?

What's a good way to handle url parameters types?

What is a good way to handle a version number in a Java application?

React JS - What is a good way to handle children click event?

Rails ActiveJob - What's the good way to handle exception in ActionMailer::DeliveryJob

What is the proper way to read numeric data (saved in a text file) into python?

Python, handle exceptions caused by file ran by other file in the other file?

What is a good way to do countif in Python

What is a good way of mapping arrays in Python?

python what's a good way to assign multiple variables from CSV file?

The correct way to handle Exceptions in a Python2.7 context manager class