1

In below code if execute in windows platform i am getting output

import subprocess
COMMAND = " Application.exe arg1 arg2"
process = subprocess.Popen(COMMAND, stdout=subprocess.PIPE, stderr=None, shell=True)

while process.poll() is None:
 output = process.stdout.readline()
 print output,

Output> Some text

But if i use shell=False I am not getting output how to get response in this case .

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
user1891916
  • 951
  • 1
  • 8
  • 9
  • as I said yeasterday: you don't need `process.poll()` here. Use [this code instead](http://stackoverflow.com/a/17698359/4279) – jfs Aug 03 '15 at 21:27

1 Answers1

2

When you set shell=False, you must provide COMMAND as list containing program name and arguments:

With shell=True:

COMMAND = "Application.exe arg1 arg2"

With shell=False:

COMMAND = ["Application.exe", "arg1", "arg2"]

I would recommend you to avoid using subprocess.Popen with shell=True in any case for security measures and use communicate() instead whenever possible:

>>> import subprocess
>>> COMMAND = " Application.exe arg1 arg2".strip().split()
>>> process = subprocess.Popen(COMMAND, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)

>>> output, error = process.communicate()
>>> print output
Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119
  • On Windows, you can pass the command as a string (it is a native interface). The issue might be the leading space in the command. – jfs Aug 03 '15 at 21:27