Take result Print and execute program

1

Dear users of the community, I have a code that allows me to search the path of a file within the system, plus what I need is to take the route that prints in order to run the program automatically, that is, if the user does not have the program installed in the default route, the program must search for the route, print it and then with the result open the program. this is the code I have

def buscararchivo(): for root, dirs, files in os.walk("c:\"): for files in files: if files == "WINWORD.EXE": print (os.path.join(root,files)) quit()

in advance I appreciate your help

    
asked by Franco M 16.03.2018 в 18:04
source

2 answers

0

I understand that what you ask is how to achieve, by typing something in a windows terminal, the result is that WINWORD.EXE is executed, for which your script searches the entire file system and you need to connect the result with the command interface.

The method you use to find WINWORD does not seem to me the most appropriate one (a recursive search for all the folders in the system can take a long time, it would be better to restrict the search to some "usual suspicious" folders like Program Files ). But leaving this aside, you have two alternatives:

  • If the command is launched from a terminal that uses a unix-type shell (for example, a cygwin or Git-Bash terminal), then you can use the capabilities of that shell. If, as part of a shell command, you enclose something between "backticks" ( ' ) what goes into them is executed and what would come out of the standard output is used as part of the command in question. It's a bit difficult to explain, but the idea would be:

    bash$ 'python tu_script.py'
    

    ( bash$ is the system prompt, not something you have to type)

  • If the command interpreter is the CMD of windows, I do not know how to do that, but in this case the simplest thing is for your own python script to launch the program once it is found, instead of showing on the screen your route.

    For this it is enough to change the print() by os.system()

answered by 16.03.2018 в 18:19
0

An observation first, you should not do for files in files: , at least it is confusing, instead you can do for file in files: .

Regarding your problem, if you want to launch "WINWORD.EXE" when it is found in the file system you can make it run in a new process using os.startfile without problems as you say:

import os

def buscar_y_ejecutar():
    for root, dirs, files in os.walk("c:/"):
        for file in files:
             if file == "WINWORD.EXE":
                 path = os.path.join(root, file)
                 os.startfile(path)
                 quit()


buscar_y_ejecutar()

Or via subprocess if you want more control over the process from the Python script (step of parameters, redirection of stdin / stdout / stderr , confirmation of completion, etc):

import subprocess

def buscar_y_ejecutar():
    for root, dirs, files in os.walk("c:/"):
        for file in files:
             if file == "WINWORD.EXE":
                 path = os.path.join(root, file)
                 subprocess.run([path])
                 quit()

buscar_y_ejecutar()

You can pass arguments if the executable needs them or accepts them without problems, for example:

subprocess.run(["c:/Program Files/Mozilla Firefox/firefox.exe",
                "www.google.com",
                "es.stackoverflow.com"])

If you want to open a file with the default program in Windows, you can simply:

path = "D:/Documentos/archivo.docx"
os.startfile(path)

What should open the file.docx file with the default application, for example M.Word.

    
answered by 16.03.2018 в 19:00