3

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 full path for every program/software in my case.

The second one that I'm currently using

import os
os.system(fileName+'.exe')

In second example problem is:-

  1. If I want to open calculator so its .exe file name is calc.exe and this happen for any other programs too (And i dont know about all the .exe file names of every program).
  2. And assume If I wrote every program name hard coded so, what if user installed any new program. (my program wont able to open that program?)

If there is no other way to open programs in python so Is that possible to get the list of all install program in user's computer. and there .exe file names (like:- calculator is calc.exe you got the point).

If you want to take a look at code

Note: I want generic solution.

Rajnish Rajput
  • 461
  • 1
  • 4
  • 13
  • Possible duplicate of [Calling an external command in Python](https://stackoverflow.com/questions/89228/calling-an-external-command-in-python) – John Szakmeister Jan 28 '18 at 11:29
  • 1
    It looks like you’re asking several questions here: 1) how do I launch a program without the full path or knowing if it ends with “exe”? 2) how do I get a list of installed programs? The problem here is that combined this is too broad of a question. You’ll see naive attempts at scanning the file system for the latter, but you shouldn’t use that as a solution. You could use subprocess.call() but pass shell=True to have it look at the PATH and COMSPEC. You should really narrow the question down. What is it that you are trying to implement? – John Szakmeister Jan 28 '18 at 11:46
  • I'm trying to get all installed programs in users PC and call them whenever I want to. But don't want to do it manually, like I don't want to gave a path or .exe file name hardcoded, if it is possible? – Rajnish Rajput Jan 29 '18 at 03:58
  • I'd recommend against that. You never know what a random executable could do. It could cause data corruption, data loss, may contain a virus, could be a service the user doesn't want to run, etc. I'd re-think your strategy. Sorry! – John Szakmeister Jan 29 '18 at 11:04
  • Yes I can understand but I'll check executable file before run. :) I'm not just gonna run all .exe files I just need them. – Rajnish Rajput Jan 29 '18 at 11:45

3 Answers3

2

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.

Jamie Crosby
  • 112
  • 6
  • It seems you did not read my whole question (I want that automated) – Rajnish Rajput Jan 28 '18 at 06:57
  • I did read your whole question. The automation part of it shouldn't be that difficult to figure out. Using a dict or a list of lists you could easily automate it depending on how you want to interface with it, via a tkinter interface, a PyQT interface, wxPython, or the command line. If you're not sure how to do that, you'll probably want to look into the os module and how to track program directories. Python will pull all these values into your program as strings which are easily passed via the various types of data structures (dict, list, set, tuple, etc.). Your question is very broad – Jamie Crosby Jan 28 '18 at 07:03
  • @RajnishRajput you haven’t mentioned anything about automating or what exactly you want automated. – John Szakmeister Jan 28 '18 at 11:36
1

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
Pratik Kumar
  • 2,000
  • 10
  • 36
1

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)
Abdul Moiz
  • 11
  • 1