Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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; '"""': 0.05; 'emulate': 0.07; 'method,': 0.07; 'python': 0.09; 'argument:': 0.09; 'command.': 0.09; 'res': 0.09; 'sep': 0.09; 'subject:command': 0.09; 'target,': 0.09; 'cc:addr:python-list': 0.10; '"<>': 0.16; 'errors)': 0.16; 'fine.': 0.16; 'received:192.168.8': 0.16; 'subject:run': 0.16; 'wrote:': 0.17; 'shell': 0.18; 'input': 0.18; 'skip:p 30': 0.20; 'all,': 0.21; 'either.': 0.22; 'cc:2**0': 0.23; 'work.': 0.23; 'this:': 0.23; 'cc:no real name:2**0': 0.24; 'command': 0.24; 'tried': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'wonder': 0.27; "doesn't": 0.28; 'subject:like': 0.29; 'fri,': 0.30; 'figure': 0.30; 'code': 0.31; 'hi,': 0.33; 'false': 0.35; 'pm,': 0.35; 'but': 0.36; 'child': 0.36; 'skip:p 20': 0.36; 'communicate': 0.37; 'subject:: ': 0.38; 'skip:o 20': 0.38; 'received:192': 0.39; 'received:192.168': 0.40; 'provide': 0.62; 'behavior': 0.64; 'skip:\xe5 10': 0.65; 'subject:....': 0.65; '8bit%:94': 0.71; 'why?': 0.84 Date: Sat, 29 Sep 2012 08:48:03 +0800 From: =?UTF-8?B?5Y+25L2R576k?= User-Agent: Mozilla/5.0 (Windows NT 5.2; WOW64; rv:9.0) Gecko/20111222 Thunderbird/9.0.1 MIME-Version: 1.0 To: Kushal Kumaran Subject: Re: how to run shell command like "< In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-CM-TRANSID: PtOowEB5EkRGRWZQX_I5AA--.3082S2 X-Coremail-Antispam: 1Uf129KBjvJXoW7uF1rCF13AF4xArWUur47twb_yoW8Ar4DpF 1UC3W7K34rGayjyry2y39Y9r4YvF1vkr43Grnxtryqyas0vFyIgFs3Jr1SvFy7Xr1a9a1U tr98Kws3J3Z7Gw7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07UHmh7UUUUU= X-CM-SenderInfo: 11ho50hxtx0q5hlv00oofrz/1tbiCgg5SU22IcONuAAAse Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 60 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1348879700 news.xs4all.nl 6983 [2001:888:2000:d::a6]:40099 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:30442 于 2012-9-28 16:16, Kushal Kumaran 写道: > On Fri, Sep 28, 2012 at 1:15 PM, 叶佑群 wrote: >> Hi, all, >> >> I have the shell command like this: >> >> sfdisk -uM /dev/sdb<< EOT >> ,1000,83 >> ,,83 >> EOT >> >> >> I have tried subprocess.Popen, pexpect.spawn and os.popen, but none of >> these works, but when I type this shell command in shell, it is works fine. >> I wonder how to emulate this type of behavior in python , and if someone can >> figure out the reason why? >> >> The sample code of subprocess.Popen is: >> >> command = ["sfdisk", "-uM", target, "<> ",", 1000, ",", "83", "\r\n", >> ",", ",", "83", "\r\n", "EOT", "\r\n"] >> >> pobj = subprocess.Popen (command, bufsize=1, \ >> stderr=subprocess.PIPE, stdout=subprocess.PIPE) >> >> res = pobj.stderr.readline () >> if res is not None and pobj.returncode != 0: >> observer.ShowProgress (u"对设备 %s 分区失败!" % target) >> return False >> > The "< command. If you use the communicate method, you can provide input as > an argument: > > command = ["sfdisk", "-uM", target ] > instructions = """ > ,1000,83 > ,,83 > """ > pobj = subprocess.Popen(command, stdin=subprocess.PIPE, > stdout=subprocess.PIPE, stderr=subprocess.PIPE) > (output, errors) = pobj.communicate(instructions) I tried this, but it is still not work. > >> and pexpect code is: >> >> child = pexpect.spawn ("sfdisk -uM /dev/sdb<> child.sendline (....) >> child.sendline (....) >> child.sendline (....) >> >> and os.popen like this: >> >> os.popen ("sfdisk -uM /dev/sdb<> >> I tried "\r\n", and it doesn't work either. >>