0
votes

When i am running a program in the console, i get some text output. When i am running the same program in Popen(..), with the same parameters, stdout and stderr are empty.

I tried everything i could imagine like shell=False and shell=True, set stdout=subprocess.PIPE, did a os.chdir() to change into the directory of this program, try p.wait() and p.communicate(), set the command as a list and as a string, but nothing works.

example:

p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
out, err = p.communicate()

--> out and err are empty strings, but if i ran this command in console i get a real output. Command is with fullpath, so its regardless where the command will be started.

My question is, are there mechanisms for programms to detect they weren't run in a real console? If so, how can i cheat.

Or miss i something?

(Python 2.7.8. x32 in Win7 x64)

1
I see this in the docs: "On Windows with shell=True, the COMSPEC environment variable specifies the default shell. The only time you need to specify shell=True on Windows is when the command you wish to execute is built into the shell (e.g. dir or copy). You do not need shell=True to run a batch file or console-based executable." - glenn jackman
Thanks @glennjackman, so i can turn shell to False :) - Duncan MC Leod
Don't know why, but after i turned on the Windows-service 'COM+ System Application', the out was filled. Now everything work fine. Even after shutdown and disabling of this service and after a restart of the pc. o.O - Duncan MC Leod

1 Answers

0
votes
from subprocess import Popen, STDOUT, PIPE
p = Popen(command, shell=True, stdout=PIPE, stderr=STDOUT, stdin=PIPE)
while p.poll() is None:
    print(p.stdout.read())
p.stdout.close()
p.stdin.close()

Try this and see if it makes any difference. Also make sure command is a string and not a list/touple, shell=True for whatever reason works better or only with strings. Also note that shell=True will get you hanged because it's insecure etc.

Also skipping .communicate() you'll need to tap off stdout otherwise the buffer will get full and you might hang both yours and the child process.


If this doesn't work, please provide more information. Such as the command used and the expected output (at least first few lines)