簡體   English   中英

嘗試在 python 中讀取文件時處理異常的好方法是什么?

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

我想在 python 中讀取 .csv 文件。

  • 我不知道文件是否存在。
  • 我目前的解決方案如下。 我覺得很草率,因為兩個單獨的異常測試笨拙地並列在一起。

有沒有更漂亮的方法來做到這一點?

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

這個怎么樣:

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

我想在python中讀取.csv文件。

  • 我不知道該文件是否存在。
  • 我當前的解決方案如下。 我覺得草率,因為兩個單獨的異常測試並置在一起很尷尬。

有更漂亮的方法嗎?

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

這是一個讀/寫示例。 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"

如何在異常中添加一個“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' 並將錯誤升級到鏈中。 從頂級錯誤處理程序獲取有關錯誤的系統信息可能會更好。

我更新了 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 腳本:

n/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
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 中不存在此異常。

在 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'

添加到@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),提醒用戶!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM