Thomas Wade
Thomas Wade

Reputation: 3

Running an exe file from a specific location in Python

I want to create a python (I'm using 3.6.2 although if there's a solution on a newer version I'd be happy to update) program that randomly selects a game from a list of game titles and runs the .exe for the game. However, I don't know how to run a .exe file from a specific folder location ie C:Program Files

Upvotes: 0

Views: 622

Answers (1)

Lukas Neumann
Lukas Neumann

Reputation: 656

Basically what you need to do is: (You need to import glob, random and os)

  1. Find all .exe in the folder with file_list = glob.glob("path/to/folder/*.exe")
  2. Select a random element game = random.choice(file_list)
  3. Run it os.startfile(game)

So:

import glob
import random
import os

file_list = glob.glob("path/to/folder/*.exe")
game = random.choice(file_list)
os.startfile(game)

Upvotes: 1

Related Questions