[英]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:
我需要在 python 脚本中运行以下命令:
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.除非这是在命令 shell 中运行的,该命令是在安装程序“fds”时安装的,该程序是一个火灾动力学模拟器。 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.
我很欣赏这似乎对程序非常具体,但我假设 python 可以通过某种通用方式从不同的位置/使用不同的设置运行命令 shell。
The shortcut to the command prompt is called CMDfds and is installed in:命令提示符的快捷方式称为 CMDfds 并安装在:
"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\FDS6" “C:\ProgramData\Microsoft\Windows\开始菜单\程序\FDS6”
in the properties the target in the shortcut tab is:在属性中,快捷方式选项卡中的目标是:
"C:\Windows\System32\cmd.exe /k fdsinit" "C:\Windows\System32\cmd.exe /k fdsinit"
Not sure it will work but you can give a try at subprocess.run with shell=True
.不确定它是否会起作用,但您可以在subprocess.run中尝试使用
shell=True
。
If shell is True, the specified command will be executed through the shell.
如果 shell 为 True,则指定的命令将通过 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.
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目录。
Also try running the python script from the fds command shell.还可以尝试从 fds 命令 shell 运行 python 脚本。 It seems to be initializing stuff in the shell.
它似乎正在初始化 shell 中的东西。
The trouble with running programs with system commands is that they often have a different shell environment.使用系统命令运行程序的问题在于它们通常具有不同的 shell 环境。 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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.