试试这个:
from subprocess import Popen, check_output, check_call, PIPE, call
get_input = input("What Should I do?")
if get_input.strip().lower() == "run":
your_exe_file_address = r'"C:\Users\you\Desktop\my_file.exe"' # example
your_module_address = r'"C:\Users\you\Desktop\test.m"' # example
your_command = "call"
process = Popen([your_exe_file_address, your_command, your_module_address], stdout=PIPE, stderr=PIPE, shell=True)
stdout, stderr = process.communicate()
# < Other Ways >
# process = check_output([your_exe_file_address, your_command, your_module_address])
# process = check_call([your_exe_file_address, your_command, your_module_address], shell=True)
# process = call([your_exe_file_address, your_command, your_module_address], stdout=PIPE, stderr=PIPE, shell=True)
print(stdout, stderr)
else:
print("Invalid Input")
另一种方式:
import os
get_input = input("What Should I do?")
if get_input.strip().lower() == "run":
your_exe_file_address = r'"C:\Users\you\Desktop\my_file.exe"' # example
your_module_address = r'"C:\Users\you\Desktop\test.m"' # example
your_command = "call"
last_shell = your_exe_file_address + " " + your_command + " " + your_module_address
os.system(last_shell)
else:
print("Invalid Input")
第三种方式(在 Windows 上,安装 pywin32 包):
import win32com.client
get_input = input("What Should I do?")
if get_input.strip().lower() == "run":
your_exe_file_address = r'"C:\Users\you\Desktop\my_file.exe"' # example
your_module_address = r'"C:\Users\you\Desktop\test.m"' # example
your_command = "call"
last_shell = your_exe_file_address + " " + your_command + " " + your_module_address
shell = win32com.client.Dispatch("WScript.Shell")
shell.Run(last_shell)
else:
print("Invalid Input")
第四种方式:
将您的命令保存在 .bat 文件中,如下所示:
"C:\Users\you\Desktop\my_file.exe" call "C:\Users\you\Desktop\test.m"
然后尝试启动这个 bat 文件并获取它的输出:
import os
get_input = input("What Should I do?")
if get_input.strip().lower() == "run":
your_bat_file_address = r'"C:\Users\you\Desktop\my_bat.bat"' # example
os.startfile(your_bat_file_address)
else:
print("Invalid Input")
祝你好运...