简体   繁体   English

如何获取 python 脚本以在 Windows (10) 中运行命令 shell (10),它在程序特定位置打开?

[英]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.

相关问题 如何仅通过在 Windows 10 cmd 行上键入脚本名称来运行 python 程序? - How do i run a python program just by typing the script name on windows 10 cmd line? 如何在 pypy3 上运行 python 脚本而不使用 windows 10 上的命令行? - How do I run a python script on pypy3 without using the command line on windows 10? 如何在Windows 10中从常规命令提示符处激活python anaconda并运行脚本 - How do I activate python anaconda and run script from regular command prompt in Windows 10 如何在 Windows 7 的命令提示符中运行 Python 程序? - How do I run a Python program in the Command Prompt in Windows 7? python中的shell命令运行特定的Windows可执行文件 - shell command in python to run a specific windows executable 如何在命令提示符Windows 10中运行python脚本 - How to run python script in Command Prompt Windows 10 如何在Windows10中从bash提示符运行python'__main__'程序文件? - How do I run python '__main__' program file from bash prompt in Windows10? 如何在 WINdows 10 中打开 Python IDLE (Shell WIndow)? - How do I open Python IDLE (Shell WIndow) in WIndows 10? Windows 10-如何确保只要打开计算机,Python脚本就能运行? - Windows 10- how do I ensure Python script will run as long as computer is on? 如何从 windows 命令提示符在虚拟环境中运行 python 程序? - How do I run a python program in a virtual environment from windows command prompt?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM