Reading in external file - error checking and line numbers...

Fredrik Lundh fredrik at pythonware.com
Wed Sep 7 05:36:15 EDT 2005


Hugh Macdonald wrote:

> I'm writing a tool at the moment that reads in an external file (which
> can use any Python syntax)
>
> At the moment, I'm reading the file in using:
>
> scriptLines = open(baseRippleScript).read()
> exec scriptLines
>
> However, if I raise an exception in my main code, in a function that is
> called from the external script, the stack trace just has:
>
> File "<string>", line 8, in ?
>
> Ideally, I'd want to be able to avoid throwing exceptions and would
> like to, from my main code, print out an error that included the script
> name (easily accessible) and the line number (less easily accessible).

    exec compile(scriptLines, baseRippleScript, "exec")

(but in this case, you might as well use the "execfile" built-in)

</F> 






More information about the Python-list mailing list