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


Groups > comp.lang.python > #24892 > unrolled thread

how to interact with Windows cmd?

Started by"self.python" <howmuchistoday@gmail.com>
First post2012-07-04 20:10 -0700
Last post2012-07-10 22:29 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  how to interact with Windows cmd? "self.python" <howmuchistoday@gmail.com> - 2012-07-04 20:10 -0700
    Re: how to interact with Windows cmd? Nobody <nobody@nowhere.com> - 2012-07-05 17:11 +0100
    RE: how to interact with Windows cmd? "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-07-10 22:29 +0000

#24892 — how to interact with Windows cmd?

From"self.python" <howmuchistoday@gmail.com>
Date2012-07-04 20:10 -0700
Subjecthow to interact with Windows cmd?
Message-ID<5c91bf76-36de-4759-938a-92a9e801e38e@g4g2000pbn.googlegroups.com>
what I want to do is
1.open cmd
2.waiting for user's typing
3.when I type "dir"
4.print the result of "dir"
5.then I type some other commands, printing the result until I type
'exit'

I used
p=subprocess.Popen('cmd',stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
p=communicate('dir')

it shows the first result but the problem is
1. it's too long so the cmd split the result with "more?", so result
is not perfect
2. after this, I typed like "cd .." but I/O is already closed so I
can't do another things..

Is there any good way?

[toc] | [next] | [standalone]


#24933

FromNobody <nobody@nowhere.com>
Date2012-07-05 17:11 +0100
Message-ID<pan.2012.07.05.16.11.32.514000@nowhere.com>
In reply to#24892
On Wed, 04 Jul 2012 20:10:47 -0700, self.python wrote:

> 2. after this, I typed like "cd .." but I/O is already closed so I
> can't do another things..

Don't use .communicate() if you want to keep the child process alive.
Write to p.stdin and read p.stdout and p.stderr.

In general, you'll need to use a separate thread for each stream,
otherwise you risk deadlock. Look at the source code for the
.communicate() method for an example.

Also, unless you specifically need stdout and stderr to be separated, use
"stderr=subprocess.STDOUT". The problem with separating them is that
there's no way to determine the order in which data was written, so
there's no way to reconstruct the output as it would have appeared on the
console.

[toc] | [prev] | [next] | [standalone]


#25159

From"Prasad, Ramit" <ramit.prasad@jpmorgan.com>
Date2012-07-10 22:29 +0000
Message-ID<mailman.2002.1341959383.4697.python-list@python.org>
In reply to#24892
> what I want to do is
> 1.open cmd
> 2.waiting for user's typing
> 3.when I type "dir"
> 4.print the result of "dir"
> 5.then I type some other commands, printing the result until I type
> 'exit'
> 
> I used
> p=subprocess.Popen('cmd',stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=s
> ubprocess.PIPE,shell=True)
> p=communicate('dir')
> 
> it shows the first result but the problem is
> 1. it's too long so the cmd split the result with "more?", so result
> is not perfect
> 2. after this, I typed like "cd .." but I/O is already closed so I
> can't do another things..
> 
> Is there any good way?

Not much experience with subprocess, but from what I have read
on here shell=True is usually bad. If you are trying to use 
Python as a sort of bash replacement, you may want to take a 
look at iPython http://en.wikipedia.org/wiki/Ipython. 

Not Apple related!

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web