我正在尝试创建一个可以从我的计算机运行.exe文件的脚本。我想我使用了错误的命令。我尝试了所有其他命令,如import os, os.startfile
,但它们无效。
这是我的代码:
loop=0
while loop==0:
answer=raw_input("coded by: Narralol\n\n"
"Pick a task:\n"
"1) Start Minecraft.exe\n"
"2) Start Minecraft_Server.jar\n"
"3) Backup your server\n"
"4) Copy the backup to your desktop\n"
"5) Exit\n")
if answer==1:
execfile('D:\Games\Minecraft\Minecraft.exe')
elif answer==2:
execfile('D:\Games\Minecraft\Minecraft_server.jar')
elif answer==5:
loop=1
答案 0 :(得分:3)
使用subprocess
module运行外部命令:
import subprocess
subprocess.call('D:\Games\Minecraft\Minecraft.exe')
答案 1 :(得分:1)
你可以这样使用os.system()(注意:使用subprocess
来做这样的事情通常会更好):
answer = 0
while answer < 5:
answer = int(raw_input("coded by: Narralol\n\n"
"Pick a task:\n"
"1) Start Minecraft.exe\n"
"2) Start Minecraft_Server.jar\n"
"3) Backup your server\n"
"4) Copy the backup to your desktop\n"
"5) Exit\n").strip())
if answer == 1:
os.system('D:\Games\Minecraft\Minecraft.exe')
elif answer == 2:
os.system('D:\Games\Minecraft\Minecraft_server.jar')
elif answer == 5:
break
更改了代码中的一些其他小问题,例如检查另一个int的int(而不是针对int的字符串)等。