簡體   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?

我需要在命令提示符下運行命令,但它們僅在命令提示符設置在系統中的特定位置時才起作用。 我需要在 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM