examples/advanced/open_file_exception.py
import sys

if len(sys.argv) != 2:
    sys.stderr.write("Usage: {} FILENAME\n".format(sys.argv[0]))
    exit()


try:
    print('** Opening file')
    fh = open(sys.argv[1])
    content = fh.read()
except IOError as e:
    print(e.errno)
    print(e.strerror)
    print(e.args)
    print(e)
else:
    print('** Closing file')
    fh.close()

python open_file_exception.py open_file_exception.py

examples/advanced/open_file_exception.out
** Opening file
** Closing file

python open_file_exception.py abcd

examples/advanced/open_file_exception.err
pening file
2
No such file or directory
(2, 'No such file or directory')
[Errno 2] No such file or directory: 'abcd'