Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
382 views
in Technique[技术] by (71.8m points)

python - How do I raise a FileNotFoundError properly?

I use a third-party library that's fine but does not handle inexistant files the way I would like. When giving it a non-existant file, instead of raising the good old

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

it raises some obscure message:

OSError: Syntax error in file None (line 1)

I don't want to handle the missing file, don't want to catch nor handle the exception, don't want to raise a custom exception, neither want I to open the file, nor to create it if it does not exist.

I only want to check it exists (os.path.isfile(filename) will do the trick) and if not, then just raise a proper FileNotFoundError.

I tried this:

#!/usr/bin/env python3

import os

if not os.path.isfile("nothing.txt"):
    raise FileNotFoundError

what only outputs:

Traceback (most recent call last):
  File "./test_script.py", line 6, in <module>
    raise FileNotFoundError
FileNotFoundError

This is better than a "Syntax error in file None", but how is it possible to raise the "real" python exception with the proper message, without having to reimplement it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Pass in arguments:

import errno
import os

raise FileNotFoundError(
    errno.ENOENT, os.strerror(errno.ENOENT), filename)

FileNotFoundError is a subclass of OSError, which takes several arguments. The first is an error code from the errno module (file not found is always errno.ENOENT), the second the error message (use os.strerror() to obtain this), and pass in the filename as the 3rd.

The final string representation used in a traceback is built from those arguments:

>>> print(FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), 'foobar'))
[Errno 2] No such file or directory: 'foobar'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...