osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

What's wrong on using Popen's communicate method?


On Thu, Jul 4, 2019 at 11:31 AM <jfong at ms4.hinet.net> wrote:
>
> Chris Angelico? 2019?7?4???? UTC+8??8?37?13????
> > On Thu, Jul 4, 2019 at 10:01 AM <jfong at ms4.hinet.net> wrote:
> > >
> > > I have the test0.py below. I expect to see 'abcd' showing in the notepad window:
> > > ---------
> > > import subprocess as sp
> > > p0 = sp.Popen('notepad.exe', stdin=sp.PIPE)
> > > p0.communicate(input=b'abcd')
> > > ---------
> > > But nothing happens. The notepad is completely empty. What have I missed?
> > >
> >
> > The "communicate" method sends text to the standard input pipe. This
> > has nothing to do with the GUI, and most Windows GUI programs take no
> > notice of it. You'll need something GUI-aware for this.
> >
> > Is Notepad just an example, or are you actually trying to control MS Notepad?
> >
> > ChrisA
>
> Yes, the notepad is just an example. My real attempt is to operate the external programs through Python. I know there are some "keyboard simulation" packages in Pypi which may work on this situation. But I prefer not bother to install them if Python's build-ins can do it.
>
> By the way, after Popen invokes an external program, is there a way of making it on-foucs when there are multiple Popen instances?
>

Definitely look into GUI manipulation tools. What you're doing is
nothing to do with the subprocess module (at least, not with the way
most Windows apps are built).

ChrisA