how to interact with Windows cmd?

Discussion in 'Python' started by self.python, Jul 5, 2012.

  1. self.python

    self.python Guest

    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?
     
    self.python, Jul 5, 2012
    #1
    1. Advertising

  2. self.python

    Nobody Guest

    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.
     
    Nobody, Jul 5, 2012
    #2
    1. Advertising

  3. > 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.
     
    Prasad, Ramit, Jul 10, 2012
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Achim Domma (Procoders)

    read input for cmd.Cmd from file

    Achim Domma (Procoders), Jun 3, 2005, in forum: Python
    Replies:
    2
    Views:
    8,115
    Peter Otten
    Jun 3, 2005
  2. Sarir Khamsi

    Interpreter-like help in cmd.Cmd

    Sarir Khamsi, Jun 9, 2005, in forum: Python
    Replies:
    4
    Views:
    390
    Bengt Richter
    Jun 26, 2005
  3. =?ISO-8859-1?Q?Sch=FCle_Daniel?=

    [exec cmd for cmd in cmds]

    =?ISO-8859-1?Q?Sch=FCle_Daniel?=, Mar 8, 2006, in forum: Python
    Replies:
    3
    Views:
    406
    Scott David Daniels
    Mar 8, 2006
  4. Diez B. Roggisch

    pydb remote debugging/cmd.Cmd over socket?

    Diez B. Roggisch, May 28, 2008, in forum: Python
    Replies:
    2
    Views:
    570
    Diez B. Roggisch
    May 29, 2008
  5. me
    Replies:
    0
    Views:
    237
Loading...

Share This Page