0

我正在尝试制作一个可以从我的计算机运行 .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
4

2 回答 2

3

使用该subprocess模块运行外部命令:

import subprocess

    subprocess.call('D:\Games\Minecraft\Minecraft.exe')
于 2012-09-21T21:31:57.357 回答
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(而不是 string 与一个 int)等。

于 2012-09-21T21:35:50.897 回答