-3

I am having problems with running some cmd commands from python script. Found lot of examples and stuff explaining subprocess.run but can't get my scripts running...

I have batch script with commands like:

set PATH=%PATH%;"C:\d\program\program_4\bin"
set PROGRAM_WAS_RUNNING=0

echo Starting PROGRAM (headless)
programd --file "C:\d\projects\project1\tool\program.exe" --dont-breakaway-from-job
if %ERRORLEVEL%==0 goto next0
    echo -- PROGRAM with GUI is running
    set PROGRAM_WAS_RUNNING=1

:next0

.. etc

Tried with this code but won't work:

command = subprocess.run(["set", "PATH=%PATH%;C:\d\program\program_4\bin"])
command = subprocess.run(["set", "PROGRAM_WAS_RUNNING=0"])

Can someone please give an example and explain in short what would be the best way to translate all of these and other similar batch commands into Python? Thanks,

John
  • 230
  • 2
  • 12
  • Please check out this method https://datatofish.com/command-prompt-python/ – Sam Varghese Jun 01 '21 at 12:18
  • `set` isn't a program, it's a [command specific to the `cmd` shell](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/set_1). Setting environment variable in Python [requires a completely different method](https://stackoverflow.com/questions/5971312/how-to-set-environment-variables-in-python). You are not being clear about what/why exactly you are trying to do, are you trying to port this batch file to Python, or are you trying to run it directly through Python? – metatoaster Jun 01 '21 at 12:23
  • I am trying to run all of batch scripts commands directly from python script – John Jun 01 '21 at 12:26

2 Answers2

0

It's Simple

    import os
    os.system("your commands here")

Notepad Example:

>>> import os
>>> os.system("notepad.exe")
0
>>> 
Adarsh Raj
  • 325
  • 4
  • 17
-1

It's import to study 'os' module. It provides some functions to facilitate the interface with Operating System like

os.putenv(key, value)
os.getenv(key, default=None)
os.uname()
os.system(command)