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.