0

I want to be able to start an application for example Notepad or Chrome using a python script. I have tried to use the os.startfile() function :

import os
os.startfile('Notepad')

It works but when I run a cmd command through it for example whoami :

import os
os.startfile('whoami')

This opens a window that automatically closes. Another thing is when using the subprocess module and opening the application from there, the program hangs. It waits for me to close the program I opened before continuing execution. I want to be able to run applications while being able to run cmd commands and storing the output in a variable without the program waiting for me to exit the application it opened, all in a single function. How can I achieve this with the criteria I have set in mind

  • 1
    Possible duplicate of [Calling an external command in Python](https://stackoverflow.com/questions/89228/calling-an-external-command-in-python) – GPhilo Sep 11 '17 at 08:40
  • No, the terminal will 'hang' until you terminate the application you opened. I dont want the terminal to hang, I want it to execute without hanging. –  Sep 11 '17 at 08:45
  • Then have a look at https://stackoverflow.com/questions/21936597/blocking-and-non-blocking-subprocess-calls – GPhilo Sep 11 '17 at 08:48
  • The answer does not fulfill the requirements I had in mind. –  Sep 11 '17 at 08:58
  • Then you should try and put those requirements in your question, because all I see is a duplicate of other existing questions. Clarifying what the expected behaviour is will improve your chances of finding a solution – GPhilo Sep 11 '17 at 09:02

1 Answers1

0

You can do it with the standard library subprocess

import subprocess

subprocess.Popen(prog)
print('This is some real multi-tasking!')

Notice that the print statement happens which the program is running.

EDIT

Okay, try this:

import subprocess

proc = subprocess.Popen('whoami', stdout=subprocess.PIPE)
output = proc.stdout.read()
print(output.decode())
Coolq B
  • 344
  • 5
  • 9
  • running this with prog being the whoami command will cause the terminal to sort of hang until I press enter, I want to avoid that at all costs and I cant store the output of the command or read from it. –  Sep 11 '17 at 08:55
  • Try the new answer :) – Coolq B Sep 11 '17 at 09:10