118

我想在 python 中读取一个 .csv 文件。

  • 我不知道该文件是否存在。
  • 我目前的解决方案如下。我觉得这很草率,因为两个单独的异常测试尴尬地并列在一起。

有没有更漂亮的方法呢?

import csv    
fName = "aFile.csv"

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

7 回答 7

82

这个怎么样:

try:
    f = open(fname, 'rb')
except OSError:
    print "Could not open/read file:", fname
    sys.exit()

with f:
    reader = csv.reader(f)
    for row in reader:
        pass #do stuff here
于 2011-04-11T21:01:58.787 回答
22

这是一个读/写示例。with 语句确保无论是否抛出异常,文件对象都会调用 close() 语句。 http://effbot.org/zone/python-with-statement.htm

import sys

fIn = 'symbolsIn.csv'
fOut = 'symbolsOut.csv'

try:
   with open(fIn, 'r') as f:
      file_content = f.read()
      print "read file " + fIn
   if not file_content:
      print "no data in file " + fIn
      file_content = "name,phone,address\n"
   with open(fOut, 'w') as dest:
      dest.write(file_content)
      print "wrote file " + fOut
except IOError as e:
   print "I/O error({0}): {1}".format(e.errno, e.strerror)
except: #handle other exceptions such as attribute errors
   print "Unexpected error:", sys.exc_info()[0]
print "done"
于 2016-02-25T20:38:35.307 回答
6

如何在异常中添加“else”子句并将“with”语句放在“else”部分?像这样:

try:
  f = open(fname, 'rb')
except FileNotFoundError:
    print(f"File {fname} not found.  Aborting")
    sys.exit(1)
except OSError:
    print(f"OS error occurred trying to open {fname}")
    sys.exit(1)
except Exception as err:
    print(f"Unexpected error opening {fname} is",repr(err))
    sys.exit(1)  # or replace this with "raise" ?
else:
  with f:
    reader = csv.reader(f)
    for row in reader:
        pass #do stuff here

而不是 sys.exit(),您可以输入“raise”并将错误升级到链上。从顶级错误处理程序获取有关错误的系统信息可能会更好。

于 2021-04-06T21:29:30.960 回答
3

我更新了 Tim Pietzcker 的答案,因为它使用了不再维护的 Python 2。

我试图先编辑答案,但我得到:编辑队列已满,所以我不能。

import errno

fname = "no_such_a_file.txt"

try:
    f = open(fname, 'rb')
except OSError as e:
    if e.errno == errno.ENOENT:
        print(
            f"No such a file or directory (errno: { e.errno }):",
            fname, file=sys.stderr
        )
    else:
        # for other OS errno codes you may want to write
        # your more specific error messages which
        print(
            f"Cannot oppen file (errno: { e.errno } ):",
            fname,
            file=sys.stderr
        )
    sys.exit(os.EX_OSFILE)

with f:
    reader = csv.reader(f)
    for row in reader:
        pass #do stuff here

我还做了一些小的改进:

代码是自包含的。

您应该检查异常的errno编号,这有助于缩小错误范围

您应该将错误和日志消息写入sys.stderr而不是写入sys.stdout(默认为打印),因为这样您可以将错误消息重定向到不同的文件中。

你应该返回一个非零的退出代码(记录在这里),如果你想让你的 python 代码在 Unix 环境中可用,这是必须的,例如 shell 脚本:

#!/usr/bin/env bash
set -euo pipefail

if ./read_file.py 2> error.log
then
    echo "do stuff"
else
    exit_code=$?
    echo "file is not readable, exit code: $exit_code" > /dev/stderr
    exit $exit_code
fi
于 2021-06-29T09:12:37.410 回答
2

在 Python 3 中,IOError 是 OSError 的别名。要验证,请运行代码:

IOError is OSError
---
True

OSError 是文件 I/O 异常的父类。

      +-- OSError
      |    +-- BlockingIOError
      |    +-- ChildProcessError
      |    +-- ConnectionError
      |    |    +-- BrokenPipeError
      |    |    +-- ConnectionAbortedError
      |    |    +-- ConnectionRefusedError
      |    |    +-- ConnectionResetError
      |    +-- FileExistsError
      |    +-- FileNotFoundError
      |    +-- InterruptedError
      |    +-- IsADirectoryError
      |    +-- NotADirectoryError
      |    +-- PermissionError
      |    +-- ProcessLookupError
      |    +-- TimeoutError
OSError.__subclasses__()
---
[ConnectionError,
 BlockingIOError,
 ChildProcessError,
 FileExistsError,
 FileNotFoundError,
 IsADirectoryError,
 NotADirectoryError,
 InterruptedError,
 PermissionError,
 ProcessLookupError,
 TimeoutError,
 io.UnsupportedOperation,
 signal.ItimerError,
 socket.herror,
 socket.gaierror,
 socket.timeout,
 ssl.SSLError,
 shutil.Error,
 shutil.SpecialFileError,
 shutil.ExecError,
 shutil.ReadError,
 urllib.error.URLError,
 gzip.BadGzipFile]

因此,如果需要详细信息,请捕获 OSError 并检查确切的类。

try:
    with open('hoge') as f:
        pass
except OSError as e:
    print(f"{type(e)}: {e}")
---
<class 'FileNotFoundError'>: [Errno 2] No such file or directory: 'hoge'
于 2021-12-31T01:29:45.010 回答
1
fname = 'filenotfound.txt'
try:
    f = open(fname, 'rb')
except FileNotFoundError:
    print("file {} does not exist".format(fname))

file filenotfound.txt does not exist

异常 FileNotFoundError 在请求文件或目录但不存在时引发。对应于 errno ENOENT。

https://docs.python.org/3/library/exceptions.html
Python 2 中不存在此异常。

于 2020-06-19T06:38:00.753 回答
-14

添加到@Josh 的示例中;

fName = [FILE TO OPEN]
if os.path.exists(fName):
    with open(fName, 'rb') as f:
        #add you code to handle the file contents here.
elif IOError:
    print "Unable to open file: "+str(fName)

这样您可以尝试打开文件,但如果它不存在(如果它引发 IOError),请提醒用户!

于 2011-04-11T21:04:14.073 回答