[python-win32] Exceptions occuring when passing parameters.

Vernon Cole wnvcole at peppermillcas.com
Fri Jul 23 01:24:00 CEST 2004


Jody:
  I cannot answer your first question. It works fine here, even with the raw
- string - double - forward - slash combination, which I assumed would blow
everything away. (My environment is python 2.3.4, pywin32 build 202, and
Windows 2000)

As for how to capture the error message:

1) Why?  Just run in command line interactive mode.  If you are at a
C:\mydir> prompt with your program named X.PY in the default directory, then
just type "x.py" at the command prompt and read the error message on the
screen.

2) If you really DO need to capture the error traceback, then use the python
system, don't fight it.
syserr is a file object and can be reassigned freely. The following example
will write a list of filenames,
and then an error traceback, in the file X.TXT:

def dirtest(dir):
    import sys
    import os
    sys.stderr = myfile
    myfile.write('just before listdir function. Maindir = ' + dir +
    '\n')
    fileNameList = os.listdir(dir)
    myfile.write(str(fileNameList))

    myfile.write('\nDone\n')

myfile = file("X.txt","w")
#this will work
dirtest(r'C://Windows//system32//dllcache')
#this will fail
dirtest(r'XC://Windows//system32//dllcache')
myfile.close()

-----Original Message-----
From: Jody Burgess [mailto:jody.burgess at sewardconsulting.com]
Sent: Thursday, July 22, 2004 10:43 AM
To: python-win32 at python.org
Subject: [python-win32] Exceptions occuring when passing parameters.
Importance: High


Hi;
	I am new to python and am writting a program that accepts directory
paths
as parameters.
I am sending r'Y://mymaindir//mysubdir//my_subdir-002' as one of the
parameters. In interactive mode, when executing:

import sys
import os
maindir = r'Y://mymaindir//mysubdir//my_subdir-002'
fileNameList = os.listdir(maindir)
print fileNameList

I get results in the fileNameList list.

However, when executing the same code after passing the path as a parameter
to a python program, I get an error and am unable to see what the error
message. As you can see I am trying to capture the error into a log file.
When this code executes, the os.listdir function fails, and error is
generated and is displayed in the window. I need to be able to trap the
exact error message.

try:
    myfile.write('just before listdir function. Maindir = ' + maindir +
'\n')
    fileNameList = os.listdir(maindir)
except:
    myfile.write(sys.exc_info)
    myfile.write(sys.exc_type)
    myfile.write(sys.exc_value)
    myfile.write('error occurs')

Two questions arise:
	1) Why does the call work in interactive mode and not when
parameters are
passed to a program?
	2) How can I trap the exact traceback message that appears on the
screen?

Jody Burgess ISP
Systems Analyst
Seward Consulting Ltd.
780.702.5103
jody.burgess at sewardconsulting.com


_______________________________________________
Python-win32 mailing list
Python-win32 at python.org
http://mail.python.org/mailman/listinfo/python-win32


More information about the Python-win32 mailing list