Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #25866

Re: How to print stdout before writing stdin using subprocess module in Python?

From Peter Otten <__peter__@web.de>
Subject Re: How to print stdout before writing stdin using subprocess module in Python?
Date 2012-07-23 15:38 +0200
Organization None
References <6b50a02c-835a-41cb-83c9-8d051a516e38@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.2468.1343050694.4697.python-list@python.org> (permalink)

Show all headers | View raw


Sarbjit singh wrote:

> I am writing a script in which in the external system command may
> sometimes require user input. I am not able to handle that properly. I
> have tried using os.popen4 and subprocess module but could not achieve the
> desired behavior.
> 
> Below mentioned example would show this problem using "cp" command. ("cp"
> command is used to show this problem, i am calling some different exe
> which may similarly prompt for user response in some scenarios). In this
> example there are two files present on disk and when user tries to copy
> file1 to file2, an conformer message comes up.
> 
> proc = subprocess.Popen("cp -i a.txt b.txt", shell=True,
> stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,)
> stdout_val, stderr_val = proc.communicate() print stdout_val b.txt?
> 
> proc.communicate("y")
> 
> Now in this example if i read only stdout/stderr and prints it, later on
> if i try to write "y" or "n" based on user's input, i got an error that
> channel is closed. Can some one please help me on achieving this behavior
> in python such that i can print stdout first, then should take user input
> and write stdin later on.

I think you need something like expect:

http://pypi.python.org/pypi/pexpect

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How to print stdout before writing stdin using subprocess module in Python? Sarbjit singh <sarbjit1987@gmail.com> - 2012-07-23 06:01 -0700
  Re: How to print stdout before writing stdin using subprocess module in Python? Peter Otten <__peter__@web.de> - 2012-07-23 15:38 +0200
  Re: How to print stdout before writing stdin using subprocess module in Python? Nobody <nobody@nowhere.com> - 2012-07-23 19:29 +0100

csiph-web