I have a class that creates a "Test" object - an object based on (describes) an external test script.
The code can be found here: https://codeshare.io/5zlW0W
I use this class like this:
from test import Test
test = Test("/path/to/test")
This works perfectly well when the test file exists, but I hit the following error when it does not exist:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/user/repos/test.py", line 13, in __init__
self.version = self.get_attribute("version")
File "/home/user/repos/test.py", line 33, in get_attribute
p = subprocess.Popen([self.path, '--' + attribute], stdout=subprocess.PIPE, universal_newlines=True)
File "/usr/lib/python3.5/subprocess.py", line 947, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.5/subprocess.py", line 1551, in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'bla'
So my question comes in two parts:
- What is the best way to handle the case where the path does not exist?
- Is it okay for me to define the initial variable using functions to grab that data as I have done in
__init__
?