Exception FileNotFoundError is not defined

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
liudr
Posts: 134
Joined: Tue Oct 17, 2017 5:18 am

Exception FileNotFoundError is not defined

Post by liudr » Thu Aug 09, 2018 8:54 pm

Here is my script's snippet:

Code: Select all

def load_paras():
# Load operating parameters from default dictionary.
# The function returns a dictionary, which has keys for all parameters needed in the program.
# Any err loading parameters will result in default parameters and False.
    try:
        f = open(config_file_name, 'r')
        par = json.load(f)
        f.close()
        return (par, False)  # Return paras and False for file not found
    except FileNotFoundError as e:
        print('No configuration file found. Starting interactive session...')
        return (paras_default, True)  # Return default paras and True for file not found
While importing the whole thing, the line with except triggers an error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "Logger_1_3_2.py", line 198, in <module>
File "Logger_1_3_2.py", line 111, in load_paras
NameError: name 'FileNotFoundError' is not defined
I was able to run this script on PC so FileNotFoundError apparently exists on PC Python. Does it also exist in MicroPython? The doc says nothing about open():

http://docs.micropython.org/en/latest/p ... .html#open

How would I go about testing whether a file exists or not? Thanks.

SpotlightKid
Posts: 334
Joined: Wed Apr 08, 2015 5:19 am

Re: Exception FileNotFoundError is not defined

Post by SpotlightKid » Fri Aug 10, 2018 1:56 pm

Use OSError.

User avatar
liudr
Posts: 134
Joined: Tue Oct 17, 2017 5:18 am

Re: Exception FileNotFoundError is not defined

Post by liudr » Tue Aug 14, 2018 1:03 am

Thanks. It works on both MicroPython and PC Python.

Post Reply