Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   how to interact with Windows cmd? (http://www.velocityreviews.com/forums/t947831-how-to-interact-with-windows-cmd.html)

self.python 07-05-2012 03:10 AM

how to interact with Windows cmd?
 
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,std out=subprocess.PIPE,stderr=subprocess.PIPE,shell=T rue)
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?

Nobody 07-05-2012 04:11 PM

Re: how to interact with Windows cmd?
 
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.


Prasad, Ramit 07-10-2012 10:29 PM

RE: how to interact with Windows cmd?
 
> 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,std out=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.


All times are GMT. The time now is 07:24 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.