0

I am trying to execute commands using communicate in the terminal that i spawned.

 sitecreate_proc = subprocess.Popen(['gnome-terminal'], stdout=subprocess.PIPE, stdin=subprocess)
 out = sitecreate_proc.communicate("pwd")
 print out

the "out" variable is always empty. Displaying the terminal is necessary.

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
claw107
  • 71
  • 1
  • 9
  • As I remember communicate return a tuple, `communicate() returns a tuple (stdoutdata, stderrdata). so you can't user communicate("pwd"). gnome-terminal returns, then try to get that result, by `sitecreate_proc.communicate()[0]` for stroutdate, or `sitecreate_proc.communicate()[0]` for stderrdata – darvark Apr 08 '17 at 19:23
  • @darvark, Yes, you're right. even if i use communicate("pwd")[0] it gives me nothing. – claw107 Apr 08 '17 at 19:24
  • @darvark, Yes please post the answer :) – claw107 Apr 08 '17 at 19:35
  • Possible duplicate of [Can a GNOME application be automated? How?](http://stackoverflow.com/questions/1006193/can-a-gnome-application-be-automated-how) – ivan_pozdeev Apr 08 '17 at 19:47

2 Answers2

1

gnome-terminal is a graphical application and as one, likely doesn't use its own standard streams that it got from the parent process.

You need to run console applications instead to communicate with them -

  • either the commands themselves:

    >>> subprocess.check_output("pwd")
    '/c/Users/Ivan\n'
    
  • or an interactive shell command, then send input to it and receive responses as per Interacting with bash from python


If you just need to output stream data to the same console that python is using, you can simply write out their data as you're getting it - either automatically with tee, or by hand at appropriate moments.


If, instead, you need to launch an independent terminal emulator window on a desktop and interact with it via IPC, that's another matter entirely - namely, UI automation, and has nothing to do with standard console streams.

The most common way for that in Linux is D-Bus (there are other options outlined on the previous link). Ppl report however (as of 2012) that gnome-terminal doesn't support D-bus and you have to jump through hoops to interact with it. There is an article on controlling konsole via D-Bus though.

Community
  • 1
  • 1
ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
  • Displaying the terminal is necessary for my project. That is why i didn't directly run the commands. Sorry for the Misunderstanding. – claw107 Apr 08 '17 at 19:41
0

As I remember communicate return a tuple,

communicate() returns a tuple (stdoutdata, stderrdata)

. so you can't user communicate("pwd"). gnome-terminal returns, then try to get that result, by sitecreate_proc.communicate()[0] for stroutdate, or sitecreate_proc.communicate()[0] for stderrdata

darvark
  • 314
  • 3
  • 15
  • i can't undersatand what you want to say(Sorry for that). Can you post a code sample? – claw107 Apr 08 '17 at 19:58
  • `sitecreate_proc = subprocess.Popen(['gnome-terminal'], stdout=subprocess.PIPE) out = sitecreate_proc.communicate()[0]` will return nothing as ivan_pozdeev wrote. eventualy you can try to get bash exit status `$?`, and if it's equal to 0 then success, in other case error. More details in http://stackoverflow.com/questions/5631624/how-to-get-exit-code-when-using-python-subprocess-communicate-method – darvark Apr 08 '17 at 20:31