I am trying to open file and look for values. If the value doesn't exist, the program will display error message and quit, otherwise will return the value. If the file does not exist, it will handle the IOError. Here is my code snippet
def findValue(value):
try:
myfile = open(file, "r")
try:
for line in myfile:
if value in line:
line = do_something
return line
finally:
myfile.close()
except IOError:
print "File does not exist!"
sys.exit()
except None as e:
print "Value not found in file"
sys.exit()
When the value does not exist, I will always get None, I guess am missing something here but can't see it! Any tips?
valueandValueare different. - cdarkewithstatement : docs.python.org/2/reference/…. Second, since you return any line found, you can be sure no value is found if the control reaches the end of the for loop thus no try/catch needed there either. - Arthur HvNone? - Peter Wood