Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธี Catch IOError Exception ใน Python

IOError raised เมื่อ I/O operation ล้มเหลว (open file ไม่มี, print statement error)

ตัวอย่าง

import sys

def whatever():
    try:
        f = open("foo.txt", 'r')
    except IOError as e:
        print(e)
        print(sys.exc_type)

whatever()

ผลลัพธ์

[Errno 2] No such file or directory: 'foo.txt'