![](/img/trans.png)
[英]How do i run a python program just by typing the script name on windows 10 cmd line?
[英]How do I get a python script to run a command shell in Windows (10) where it opens in a program specific location?
我需要在命令提示符下运行命令,但它们仅在命令提示符设置在系统中的特定位置时才起作用。 我需要在 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")
系统无法识别该命令
os.system("mpiexec -n 9 FDS crit_vel_01_02.fds")
除非这是在命令 shell 中运行的,该命令是在安装程序“fds”时安装的,该程序是一个火灾动力学模拟器。 我很欣赏这似乎对程序非常具体,但我假设 python 可以通过某种通用方式从不同的位置/使用不同的设置运行命令 shell。
命令提示符的快捷方式称为 CMDfds 并安装在:
“C:\ProgramData\Microsoft\Windows\开始菜单\程序\FDS6”
在属性中,快捷方式选项卡中的目标是:
"C:\Windows\System32\cmd.exe /k fdsinit"
不确定它是否会起作用,但您可以在subprocess.run中尝试使用shell=True
。
如果 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目录。
还可以尝试从 fds 命令 shell 运行 python 脚本。 它似乎正在初始化 shell 中的东西。
使用系统命令运行程序的问题在于它们通常具有不同的 shell 环境。 为了防止由此引起的问题,最好使用绝对路径。 在你的情况下:
os.system("mpiexec -n 9 FDS crit_vel_01_02.fds")
应改为:
os.system("/absolute/path/to/mpiexec -n 9 FDS crit_vel_01_02.fds")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.