I have a small Python application that needs to load a few different files (templates and config files). It should look in a number of destinations, in the following order, and load the file from the first destination that contains such a file.
- current working directory
$MYAPP_PATH(may contain several paths,separated by colon)
I have written the following:
def open_sp(filepath): #open searchpath pathlist =  pathlist.append('.') pathlist.extend(os.environ['MYAPP_PATH'].split(':')) pathlist.append(os.path.join(os.path.abspath(os.path.dirname(__file__)),'resources')) for p in pathlist: try: return open(os.path.join(p,filepath)) except FileNotFoundError: pass raise FileNotFoundError()
And I have exported the following environment variable in shell:
$ export MYAPP_PATH=/home/jdoe/templates/:/home/jdoe/legacy_templates/
In my case, it will look for the file in the following locations:
And it seems to work. But I suspect this could be much more elegant, perhaps by using some built-in Python tool, maybe from the
Also, if considering my code, I'm not sure if FileNotFound is the proper exception to throw, since it expects a path argument, and I am not sure which of the path arguments to provide.