how to interact with Windows cmd?

S

self.python

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?
 
N

Nobody

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

Prasad, Ramit

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top