Hello i am new to python programming. I am self learning by doing a project.
the code below is working fine, but i want to catch the exception if the condition is FALSE. i already have a statement to print if the directory is not there; and continue. I know i can make this code work by nesting the if statement.
but i dont want to do. i want to proceed to next line of code. when i run this as it is, i will get an error if the directory is missing.
import os
goToDir = open("URPsetup.dat","r")
goToDirPath = goToDir.read()
if (os.path.exists(goToDirPath))== True:
os.chdir(goToDirPath)
else: print("directory not found, please check file URPsetup.dat")
goToConfig = open("utility.dat", "r")
print(goToConfig.read())
i get this error message when the directory is not there. What i mean here is that, the directory i supplied in "URPsetup.dat" was incorrect. I did this on purpose to see if the code will stop at the else statement above. it will print the else statement, and continue on to show the error message below. how do i catch this error and stop?
directory not found, please check file URPsetup.dat
Traceback (most recent call last):
File "C:/Users/R82436/PycharmProjects/URP2017/prototype.py", line 9, in <module>
goToConfig = open("utility.dat", "r")
FileNotFoundError: [Errno 2] No such file or directory: 'utility.dat'
directory not found, please check file URPsetup.dat
Process finished with exit code 1