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('')
for line in n_url.readlines():
print line

Thanks in advance for your help.

Dani AI

Generated

Two separate things need to be handled: capture the child process return code inside Python, and—only if you need the calling CMD session to see that code—have Python exit with it so %errorlevel% is set for the parent shell. Trying to read the shell variable by spawning another shell is fragile. Instead, capture the process return code directly with subprocess, send that value to your CF page, and call sys.exit(code) if you want the batch file's %errorlevel% set.

Example (Python 3):

import subprocess
import sys
from urllib import parse, request

result = subprocess.run(
    ['C:\\path\\to\\your.exe', 'arg1'],
    stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
exit_code = result.returncode

query = parse.urlencode({'ProcessID': '000', 'ErrorCode': str(exit_code)})
url = 'http://cob/gis/GISAutoProcess_Action.cfm?' + query
with request.urlopen(url) as resp:
    resp.read()

# if this script was launched from a batch file and you want CMD's %errorlevel% set:
sys.exit(exit_code)

Notes and troubleshooting

  • Use subprocess.run (or Popen + wait/communicate) to get a reliable .returncode. Avoid parsing echoed environment variables from a spawned shell.
  • Changes to os.environ do not propagate back to the parent process; only the process exit status becomes the caller's errorlevel.
  • If you run commands with shell=True you get the shell's exit status; with shell=False you get the child process status. Pick the mode you need.
  • URL-encode values (shown above). For sensitive/large payloads prefer an HTTP POST instead of putting data on the query string.

This keeps the solution deterministic: can capture the exact exit code and post it to SQL, and 's approach of querying the shell is acknowledged but replaced with a more robust method.

Hi mccarthp,

Let me see if I understand this. Windows maintains a variable called errorlevel, which contains the exit code of the most recently executed program, right? And you are trying to read that variable?

Hmm. Off the top of my head, we could try:

import os
keys = os.environ.keys()
keys.sort()
for item in keys: print item, ":", os.environ[item]

But I don't see the errorlevel variable in this list.

Well, I don't know how to query for non-environment variables in DOS, but here's a quick workaround:

import os
f = os.popen("echo %errorlevel%", "r")
text = f.read()

The value of errorlevel should be inside text, along with some whitespace, but you can take care of that yourself, then insert the code into your URL string.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.