You are not logged in.

#1 2008-10-06 13:15:47

pyther
Member
Registered: 2008-01-21
Posts: 1,395
Website

Python.... try excpet... help please

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?


Website - Blog - arch-home
Arch User since March 2005

Offline

#2 2008-10-06 13:52:54

ralvez
Member
From: Canada
Registered: 2005-12-06
Posts: 1,694
Website

Re: Python.... try excpet... help please

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

#3 2008-10-06 14:51:45

pyther
Member
Registered: 2008-01-21
Posts: 1,395
Website

Re: Python.... try excpet... help please

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.


Website - Blog - arch-home
Arch User since March 2005

Offline

#4 2008-10-06 16:04:02

lman
Member
From: CZ
Registered: 2007-12-18
Posts: 255

Re: Python.... try excpet... help please

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

#5 2008-10-06 16:18:51

Husio
Member
From: Europe
Registered: 2005-12-04
Posts: 359
Website

Re: Python.... try excpet... help please

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

#6 2008-10-06 16:32:33

pyther
Member
Registered: 2008-01-21
Posts: 1,395
Website

Re: Python.... try excpet... help please

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)


Website - Blog - arch-home
Arch User since March 2005

Offline

#7 2008-10-07 01:48:26

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: Python.... try excpet... help please

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

Board footer

Powered by FluxBB