Link to home
Avatar of Deonicus
Deonicus

asked on

Running DOS commands in Python with os.system()

I have created a batch file that creates a bootable BCD/etc. so that I can boot to a WIM image. The command looks like this:
for /f "tokens=3" %%a in ('bcdedit /create /d "WIM_X" /application OSLOADER') do set guid=%%a
I need to be able to run the commands in my batch file in a python script. I have tried this:

command = 'for /f \"tokens=3\" %%a in (\'bcdedit /create /d \"WIM_X\" /application OSLOADER\') do set guid=%%a'
When I execute this, the command window indicates that the env var is being set, i.e.
c:\set guid={blah-blah-blah}
However, the variable doesn't appear to be set in the environment (I run another command to "echo" the env var, and it is unset. I use os.environ.get(guid) and it is also unset).

My question is, how can I set and retrieve the value of guid within my python script?

Note, I am constrained to the environment I am in, I can't just run the batch file, I must use a python script to accomplish what I need.
Batch:
for /f "tokens=3" %%a in ('bcdedit /create /d "WIM_X" /application OSLOADER') do set guid=%%a
Python:
command = 'for /f \"tokens=3\" %%a in (\'bcdedit /create /d \"WIM_X\" /application OSLOADER\') do set guid=%%a'

Open in new window

Avatar of pepr
pepr

Try to set the environment variable using

  os.putenv('guid', 'value')

Then, it would probably be better to use a Python loop instead of the cmd loop.
Or probably, it is better to work with os.environ which is a Python map mapped to the environment.
import os
 
for v in os.environ:
    print v, '=', os.environ[v]
 
os.environ['myVariable'] = 'my value'
 
print 'myVariable', '=', os.environ['myVariable']

Open in new window

Avatar of Deonicus

ASKER

Thanks, pepr.
The problem is that the value of 'guid' must be set via the DOS 'for' command, then it must be used (as an environment var) in other DOS commands executed by python's os.system.
i.e., in the batch file, the next command is
bcdedit /set %guid% device ramdisk=[boot]\X_BOOT\sources\boot.wim,{ramdiskoptions}
So, in the pyhon script it looks like this:
command = 'bcdedit /set %guid% device ramdisk=[boot]\\X_BOOT\\sources\\boot.wim,{ramdiskoptions}'
I would expect that %guid% would work to get the value of the env var 'guid' when I run os.system(command), but it appears not to. The only reason that I would want to capture the value of 'guid' in the python script is so that I could string replace %guid% in the previous command with the value of the env var.
SOLUTION
Avatar of pepr
pepr

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
ASKER CERTIFIED SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
;)  When starting, you should probably focus on the subprocess module instead of os.system() and os.popen(). See http://docs.python.org/library/subprocess.html -- also the examples how os.system() and os.popen() should be replaced in a Python code.
pepr,
Thanks for the link. Naturally, I'm restricted to using python 2.3 in my process. I'm sure we'll update to a newer version of python one of these days, but it's a big process.