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

Microsoft DOSPython

Avatar of undefined
Last Comment
Deonicus

8/22/2022 - Mon
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.
pepr

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

ASKER
Deonicus

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.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
SOLUTION
pepr

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
pepr

;)  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.
ASKER
Deonicus

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.