1

Having trouble translating the following two specifications to Python code. I'm finding the terminology is strange for a Python program (throw, catch, contents of exception).

First specification states "If the file can't be opened because it does not exist, you must catch the FileNotFoundError object and throw a new exception FileNotFoundError with the contents of the exception being the filename. Any other exception for failure to open is not caught. filename is sent into a function.

I translated this as...

try:
    f = open(filename)
except FileNotFoundError(filename):
    raise FileNotFoundError(filename)

I ask as I've already said; the terminology is strange; the "contents of the exception being the filename" for example.

Also, the other specification being that if the parameter filename is not a string type, then a TypeError exception is thrown with the contents of the exception being a string "parameter filename is not a string".

Again, the "contents of the exception"?

My translation for that specification is...

x = isinstance(filename, (str))
if x == False:
    raise TypeError('parameter filename is not a string')
Grog
  • 13
  • 2

1 Answers1

0

To begin with, we do except Exception: in python, instead of except FileNotFoundError(filename): which you did

Yes, a way of thinking is that if open cannot find the file, it will throw FileNotFoundError, we can see this by just catching a general exception and printing it.

filename = 'abcd'
try:
    f = open(filename)
except Exception as e:
    print('{} {}'.format( e.__class__.__name__, e))

This will give

FileNotFoundError [Errno 2] No such file or directory: 'abcd'

e.__class__.__name__ gives us name of the exception class, and e gives a string description

Now what the correct way of doing what you were doing is

filename = 'abcd'
try:
    f = open(filename)
except FileNotFoundError:
    raise FileNotFoundError(filename)

Which means that when the open throws a FileNotFoundError, catch that specific exception, and rethrow it after modifying the exception string by doing FileNotFoundError(filename), and here we modified the exception string to be the filename

To see what is happening now, we call this function in this way

def func():

    filename = 'abcd'
    try:
        f = open(filename)
    except FileNotFoundError:
        raise FileNotFoundError(filename)

try:
    func()
except Exception as e:
    print('{} {}'.format(e.__class__.__name__, e))

Which will output FileNotFoundError abcd. We see here that the exception string or the contents as you call it, the filename is being printed as the exception string here.

Also your assumption below

x = isinstance(filename, (str))
if x == False:
    raise TypeError('parameter filename is not a string')

is sort of correct, filename excepts not only strings, but also integers as we can see in the docs: https://docs.python.org/3/library/functions.html#open

file is a path-like object giving the pathname (absolute or relative to the current working directory) of the file to be opened or an integer file descriptor of the file to be wrapped.

An example of below is

filename = 1.45
try:
    f = open(filename)
except Exception as e:
    print('{} {}'.format( e.__class__.__name__, e))

The output is TypeError integer argument expected, got float, since it tried to convert float to int, thinking of it as a file descriptor, but it got a float

Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40