Launch program with Python or with cmd

1

Good morning, I hope you can help me. The fact is that I want to launch a program with a Python script. The first thing that came to my mind was using the os module in the standard Python 3 library.

It would be something like:

import os 
prom=os.system("start chrome")

This works and, for example, opens Chrome successfully.

I use the os module in its system function to automatically type in Chrome. But in the case of not so well known programs it does not open them. You have to find the specific name of the .exe file and execute it or add it to the path. Correct me if I'm wrong. Maybe there is another way than with os .

In short, what I want is to be able to launch programs from a python script.

    
asked by limg21 19.07.2017 в 04:08
source

1 answer

1

You can get much more control and communication with the process by using subprocess.popen . Do not block your script until the process returns and you can also communicate with the process via stdin and get stderr / stdout using pipes.

An example would be:

import subprocess

subprocess.Popen(['C:/Program Files/Mozilla Firefox/firefox.exe',
                  'https://es.stackoverflow.com/',
                  '-new-tab']) 

This opens Mozilla Firefox or creates a new tab (if it is already open) showing the SOes page.

In this case, an absolute path to the executable is used. You can also call processes that are recognized by their name (see below) or use paths relative to the script that launches the process.

Neither the command start or subprocess.popen perform a search when a name of an application is provided, they simply pass it to the operating system (via API, for example using ShellExecuteEx ) that is responsible for performing a search Automatic that usually encompasses:

  • Current work directory
  • Windows Directory
  • Windows Directory \ System32
  • The directories listed in the PATH environment variable
  • Application paths defined in the registry

You can look at the following link for more information:

link

Note that there is no way to call an executable if it is not in any of the previous directories where it can be found by the OS or without providing the absolute / relative path to the executable. It's not a matter of using a library or another or one language or another, it would just be like asking "find something that can run called" example.exe "and it will be somewhere in my file system and execute it "

In your case, open Chrome because in the installation the path was added in the registry (possibly in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths ), it is not a matter of being a more or less common program.

Of course, you could create a script that would be responsible for looking for a certain executable in certain directories where you think it is possible that the executable is found and run it if it finds it. Something like the following, which is neither the safest nor the most efficient and which only aims to be a simple example :

import os
import subprocess

def ejecutar(file, directorios):
    for ruta in directorios:
        for root, dirs, files in os.walk(ruta):
            if file in files:
                subprocess.Popen([os.path.join(root, file),
                                  'https://es.stackoverflow.com/',
                                  '-new-tab'])
                return

file = 'firefox.exe'
directorios = ['C:/Program Files', 'C:/Users/Fulanito']
ejecutar(file, directorios)

All this is referred to Windows as is obvious, in * nix systems the thing is different as expected.

    
answered by 19.07.2017 / 04:34
source