You are not logged in.
Hello I am using pyfig which is a small script to parse files. I need to make a try except statement, in case a user mess up the config file.
This is what is being reported by the my script:
[Mon Oct 06 08:51:00 2008] [error] [client 10.25.11.102] File "/var/www/html/test/pyfig.py", line 81, in grab
[Mon Oct 06 08:51:00 2008] [error] [client 10.25.11.102] raise ConfigError("Specified config name was not found while grabbing")
This leads me to believe I want to catch ConfigError, however this doesn't work, it says
UnboundLocalError: local variable 'z' referenced before assignment
My code:
if os.path.isdir(path):
file = str('/home/') + user + str('/public_html/.website_info.txt')
if os.path.isfile(file):
pyfig = Pyfig(file)
try:
print pyfig.grab("name")
except ConfigError, e:
z = e
print z
So what do I need to have for the expect field to get this to work?
Offline
It seems to me the problem is the level at which you catch the error.
I would do it this way:
if os.path.isdir(path):
try:
file = str('/home/') + user + str('/public_html/.website_info.txt')
except ConfigError, e:
print ("Error: e" %e)
Offline
file = str('/home/') + user + str('/public_html/.website_info.txt'), that is just generating a path.
print pyfig.grab("name") is looking for the the line in the config that says name=Matthew
However I want a try...except in case the user accidentally misspells name in the config file. Say they type sname, this would crash my webscript, which would be terrible! Python shows raise ConfigError("Specified config name was not found while grabbing") when it can't find the setting name. however my except statment seems not be catching it.
Offline
One problem surely is, that if you don't catch an exception the z variable does not exist yet, so you can't print it...
Offline
You may want to copy the error string.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def fun():
try:
raise Exception("test exception")
except Exception, e:
exc = e[:][0]
print "Exception:", exc
if __name__ == "__main__":
fun()
http://python.net/~goodger/projects/pyc … -variables
Last edited by Husio (2008-10-06 16:22:08)
Offline
I don't think I described my problem very well.
I found out I had to add:
from pyfig import ConfigError
I thought that
import pyfig
would take care of that :-/
Thanks all for you suggestions!
Last edited by pyther (2008-10-06 16:45:45)
Offline
If you use the import pyfig syntax, your try/except should look like this:
try:
#code
except pyfig.ConfigError, e:
# exception code
Everything in your python namespace has to be explicitly imported/created unless you used the from something import * syntax... which you shouldn't for that reason.
Dusty
Offline