Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to print stdout before writing stdin using subprocess module in Python?

Reply
Thread Tools

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

 
 
Sarbjit singh
Guest
Posts: n/a
 
      07-23-2012
I am writing a script in which in the external system command may sometimesrequire 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 ifi 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.

 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      07-23-2012
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

 
Reply With Quote
 
 
 
 
Nobody
Guest
Posts: n/a
 
      07-23-2012
On Mon, 23 Jul 2012 06:01:23 -0700, Sarbjit singh wrote:

> 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.


You can't use .communicate() if you want an interact with the child
process. Any text given as an argument is sent to the child, then the
child's stdin is closed. Then it reads the child's stdout and/or stderr
until the child terminates.

If you want to interact with the child, you need to write to proc.stdin
and read from proc.stdout and/or proc.stderr. And you need to do this
asynchronously, i.e. you either need to use non-blocking I/O or multiple
threads, otherwise you risk deadlock. Look at the source code for the
..communicate() method in subprocess.py for examples (the Unix version uses
non-blocking I/O, the Windows version uses threads).

Even that may not be enough, as the child process may behave differently
if std{in,out,err} are pipes rather than a tty. Typically, stdin and
stdout are line-buffered when associated with a tty but block-buffered
otherwise (e.g. when associated with a pipe).

If pipes don't work, the only solution may be to run the child process on
a pseudo-tty (pty); see the pty module or the os.openpty() function.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Some general questions about using "stdin","stdout".... asdsd sir Python 1 02-16-2006 04:07 PM
[Subprocess/Windows] subprocess module under Windows 98 Andreas Jung Python 2 11-02-2005 05:41 PM
/dev/pts/x, /proc/x/fd, reading stdout, writing stdin, not with open3 Andreas Persson Perl Misc 2 03-03-2004 06:52 PM
Using stdin and/or stdout between two programs Harayasu C Programming 3 08-21-2003 06:33 AM



Advertisments