#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    6
    Rep Power
    0

    Question Setting a Variable to the Exit Code


    Hello, I am new to Python and I am just starting to figure things out but this one has me stumped.

    I need to capture the exit code of my processes in order to track them in a database. I have all the other components working except this. In DOS I use:

    Echo %errorlevel%

    then post the value of %errorlevel% to the SQL table by calling a CF page and passing the value in the URL.

    Is there a way to calculate %errorlevel% with the exit code of the process in Python? I need Python to set %errorlevel% equal to the exit code. Iím pretty sure that Ď%í is not the proper syntax in Python to designate a variable but, as I said, Iím new. Below is the Python code that I am using to open the CF page that inserts the data to SQL.

    opener = urllib.FancyURLopener({})
    n_url = opener.open('http://cob/gis/GISAutoProcess_Action.cfm?ProcessID=000&ErrorCode=%errorlevel%')
    for line in n_url.readlines():
    print line

    Thanks in advance for your help.
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Hmm. Your question in general is a bit confusing to me. But to call functions (I'm not sure about variables) from Batch applications in Python, you can call systems commands through the o module.
    Code:
    import os
    os.system('echo hello')
    This would be the same as typing echo hello in your Windows command prompt, or in the terminal for a Unix or Linux system. Whatever is being called in os.system() is the command you will tell the system to perform. This may help you out a bit.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    6
    Rep Power
    0
    Sorry about the confusing question. What I want to do is pass the exit code from my process to the SQL table. Right now I am opening a CF page to do this. The URL needs to pass values of the ProcessID and the ErrorCode to the CF page. The ProcessID does not change and I will simply hard code it but I want the ErrorCode to be what the result of the process is. So if there was an error I want the ErrorCode to be equal to returned exit code, usually 1, if it runs properly then equal to 0. I hope this clarifies what I am trying to accomplish. Thanks for your help.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    62
    Rep Power
    10
    Maybe you need something like this?
    Code:
    def GetConfig():
    	file = "mysql"
    	if os.path.isfile(file) == True:
    		try:
    			archivo = open(file,"r")
    			result = archivo.read()
    		except:
    			error = 100
    			return error
    		try:
    			result = string.split(result,":::")
    			if result[1] == "None": result[1] = ""
    			return result[0],result[1],result[2],result[3]
    		except:
    			error=101
    			return error
    	else:
    		error = 102
    		return error
    If the method fails, it returns an error code...
    so then I could say
    Code:
    result = GetConfig()
    if result not in [100,101,102]:
        #Do something
    And that is saying, if no errors in GetConfig, then do something...
  8. #5
  9. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    After re-reading your question, it sounds like you want to replace %errorlevel% with another value?

    Code:
    opener = urllib.FancyURLopener({})
    n_url = opener.open('http://cob/gis/GISAutoProcess_Action.cfm?ProcessID=000&ErrorCode=%s' % (errorlevel))
    for line in n_url.readlines():
    print line
    You can see that I replaced %errorlevel% with %s. %s is used for strings. Then you can see me do % (errorlevel) after the string constant. THis is putting the errorlevel variable in place of the %s. %s (%d for integers) can help a great deal. It can be put anywhere in a string constant, and when you do the % () at the end of the string, you can enter in any amount of string variables inside it, as long as there is a %s to correspond with it that is already inside the main string constant. This should clear things up.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    Originally Posted by mccarthp
    What I want to do is pass the exit code from my process to the SQL table.
    OK - as long as you don't mean the currently-running-process, as that wont have an exit code while it's still running.

    %errorlevel% in DOS/Windows NT CMD is an environment variable containing the exit code from the last running program. It's used to send a value back to the shell (or calling program), and it doesn't really make sense to be talking about %errorlevel% anywhere else.
    Convention has it that 0 is "fine" and anything else is an error, as you say.

    If you want to set this environment variable when your Python program/script/process exits, so calling programs can see whether your program was OK, use:

    Code:
    import sys
    sys.exit(0) # exit with error code 0
    If you are calling a process from your Python script, and would like to retreive its error code, then you will need something like the subprocess module:

    Code:
    import subprocess
    returncode = subprocess.call("program.exe")
    Then, you could send that return code to your database.

    The ProcessID does not change
    It will if you really mean a Windows or Linux process ID that you might see in Task Manager or ps. If you are using "Process ID" to refer to something else, then ignore this.

    I hope this clarifies what I am trying to accomplish.
    Not really - but as far as I can see you want one of:

    - Set the proper "errorlevel" from a script so something else can read it - sys.exit(n)
    - Read the proper "errorlevel" from another program when calling it from a script - subprocess.call("program.exe")
    - Call a function that does a process and returns an error code which needs to go in a database, and actually has nothing to do with environment variables and OS processes except similar terminology.

    So, I hope that's some use.

IMN logo majestic logo threadwatch logo seochat tools logo