简体   繁体   English

如何在Python中运行程序。

[英]How to Run a Program in Python.

I am trying to make a script which would run an .exe file from my computer. 我正在尝试制作一个脚本,该脚本将从我的计算机运行一个.exe文件。 I think I am using the wrong command. 我认为我使用了错误的命令。 I tried all the other commands like import os, os.startfile , but they aren't working. 我尝试了所有其他命令,例如import os, os.startfile ,但是它们不起作用。

Here is my code: 这是我的代码:

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

Use the subprocess module to run external commands: 使用subprocess模块运行外部命令:

import subprocess

    subprocess.call('D:\Games\Minecraft\Minecraft.exe')

You can use os.system() like so (note: it is usually better to use subprocess for doing things like this): 您可以像这样使用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

Changed a few other minor things in the code like checking an int against another int (instead of string against an int), etc. 更改了代码中的其他一些小事情,例如将一个int与另一个int进行比较(而不是针对int的字符串)等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM