简体   繁体   中英

How do I get a python script to run a command shell in Windows (10) where it opens in a program specific location?

I need to run commands in command prompt but they only work when the command prompt is set at a particular location in the system. I need the following commands to run in a python script:

import os
os.system("set OMP_NUM_THREADS=2")
os.system("explorer.exe /e,::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"@
os.system("cd C:\CFD\crit_vel_01_02")
os.system("mpiexec  -n  9 FDS  crit_vel_01_02.fds")
os.system("PAUSE") 

the system does not recognise the command

os.system("mpiexec  -n  9 FDS  crit_vel_01_02.fds")

unless this is run in the command shell which is installed on installation of the program "fds" which is a fire dynamics simulator. I appreciate this seems quite specific to the program but I am assuming there is some generic way that python can run command shell from a different location/with different settings.

The shortcut to the command prompt is called CMDfds and is installed in:

"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\FDS6"

in the properties the target in the shortcut tab is:

"C:\Windows\System32\cmd.exe /k fdsinit"

Not sure it will work but you can give a try at subprocess.run with shell=True .

If shell is True, the specified command will be executed through the shell. This can be useful if you are using Python primarily for the enhanced control flow it offers over most system shells and still want convenient access to other shell features such as shell pipes, filename wildcards, environment variable expansion, and expansion of ~ to a user's home directory.

Also try running the python script from the fds command shell. It seems to be initializing stuff in the shell.

The trouble with running programs with system commands is that they often have a different shell environment. In order to prevent problems arising from this it's a good idea to use absolute paths. In your case:

os.system("mpiexec  -n  9 FDS  crit_vel_01_02.fds")

should be changed to:

os.system("/absolute/path/to/mpiexec  -n  9 FDS  crit_vel_01_02.fds")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM