0

I am trying to make a script which would run an .exe file from my computer. I think I am using the wrong command. I tried all the other commands like import os, os.startfile, but they aren't working.

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
2
  • see stackoverflow.com/q/12339671/1240268 Commented Sep 21, 2012 at 21:32
  • You can edit the title :). I added the tag py2exe which I assume you are using to do this. Commented Sep 21, 2012 at 21:49

2 Answers 2

3

Use the subprocess module to run external commands:

import subprocess

    subprocess.call('D:\Games\Minecraft\Minecraft.exe')
Sign up to request clarification or add additional context in comments.

2 Comments

@JoranBeasley: Even the os.system documentation points to subprocess as the preferable method; for one, the default argument handling method avoids shell injection vulnerabilities.
yeah I know ... but he is controlling the input and doesnt need the output ... so in this case I would say os.system would work fine...but subprocess is certainly the "right" way to do it
1

You can use os.system() like so (note: it is usually better to use subprocess for doing things like this):

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.

3 Comments

Yeah, thank you very much, it's working :) I think the problem was that the answer was recognised as a string rather then an int so it wasn't executing the file. It just kept going in loop without doing anything. Maybe that could be the mistake?
That is correct, i.e.: x = "1"; y = 1; x != y; int(x) == y; x == str(y)
Oooooh yes I understand now :). That makes perfect sense!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.