Как лучше всего обрабатывать исключения при попытке чтения файла в Python?

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 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
57
задан Gerd 13 May 2019 в 11:52
поделиться