Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'url:pypi': 0.03; 'subject:Python': 0.05; 'properly.': 0.07; 'subject:How': 0.09; 'python': 0.09; 'closed.': 0.09; 'command.': 0.09; 'input,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'stdout': 0.09; 'subject:module': 0.09; 'subject:using': 0.09; '"n"': 0.16; 'exe': 0.16; 'file1': 0.16; 'input.': 0.16; 'message- id:@dough.gmane.org': 0.16; 'proc': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'stdin': 0.16; 'subject:writing': 0.16; 'subprocess': 0.16; 'later': 0.16; 'wrote:': 0.17; 'tries': 0.17; 'input': 0.18; 'module': 0.19; "user's": 0.22; 'example': 0.23; 'external': 0.24; 'command': 0.24; 'script': 0.24; 'tried': 0.25; 'header:User- Agent:1': 0.26; 'first,': 0.27; 'disk': 0.27; 'header:X -Complaints-To:1': 0.28; 'behavior.': 0.29; 'prints': 0.29; 'error': 0.30; 'up.': 0.31; 'url:python': 0.32; 'channel': 0.32; 'could': 0.32; 'print': 0.32; 'achieving': 0.33; 'singh': 0.33; 'handle': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'problem,': 0.35; 'sometimes': 0.35; 'subject:?': 0.35; 'something': 0.35; 'there': 0.35; 'received:org': 0.36; 'but': 0.36; 'url:org': 0.36; 'should': 0.36; 'skip:p 20': 0.36; 'two': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'help': 0.40; 'think': 0.40; 'mentioned': 0.63; 'different': 0.63; 'show': 0.63; 'behavior': 0.64; 'prompt': 0.78; 'subject:before': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: How to print stdout before writing stdin using subprocess module in Python? Date: Mon, 23 Jul 2012 15:38 +0200 Organization: None References: <6b50a02c-835a-41cb-83c9-8d051a516e38@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p50849bd3.dip.t-dialin.net User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1343050694 news.xs4all.nl 6985 [2001:888:2000:d::a6]:54416 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:25866 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