Skip to content Skip to sidebar Skip to footer

How To Open Any Program In Python?

Well I searched a lot and found different ways to open program in python, For example:- import os os.startfile(path) # I have to give a whole path that is not possible to give a f

Solution 1:

There's always:

from subprocess import call
call(["calc.exe"])

This should allow you to use a dict or list or set to hold your program names and call them at will. This is covered also in this answer by David Cournapeau and chobok.


Solution 2:

You can try with os.walk :

import os

exe_list=[]

for root, dirs, files in os.walk("."):
 #print (dirs)
 for j in dirs:
  for i in files:
   if i.endswith('.exe'):
     #p=os.getcwd()+'/'+j+'/'+i
     p=root+'/'+j+'/'+i
     #print(p)
     exe_list.append(p)


for i in exe_list :
  print('index : {} file :{}'.format(exe_list.index(i),i.split('/')[-1]))

ip=int(input('Enter index of file :'))

print('executing {}...'.format(exe_list[ip]))
os.system(exe_list[ip])
  1. os.getcwd()+'/'+i prepends the path of file to the exe file starting from root.
  2. exe_list.index(i),i.split('/')[-1] fetches just the filename.exe
  3. exe_list stores the whole path of an exe file at each index

Solution 3:

Can be done with winapps

First install winapps by typing:

pip install winapps

After that use the library:

# This will give you list of installed applications along with some information

import winapps 

for app in winapps.list_installed(): 
    print(app)

If you want to search for an app you can simple do:

application = 'chrome'

for app in winapps.search_installed(application): 
    print(app)

Post a Comment for "How To Open Any Program In Python?"