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

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

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

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,394
    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:
    587
    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:
    626
    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:
    751
    Diez B. Roggisch
    May 29, 2008
  5. Diez B. Roggisch

    cmd.Cmd bug or at least docu-bug

    Diez B. Roggisch, May 29, 2008, in forum: Python
    Replies:
    1
    Views:
    489
    Michele Simionato
    May 29, 2008
  6. Jean-Michel Pichavant

    python cmd.Cmd auto complete feature

    Jean-Michel Pichavant, Mar 8, 2011, in forum: Python
    Replies:
    0
    Views:
    505
    Jean-Michel Pichavant
    Mar 8, 2011
  7. Tim Chase

    cmd.Cmd asking questions?

    Tim Chase, Nov 28, 2011, in forum: Python
    Replies:
    0
    Views:
    230
    Tim Chase
    Nov 28, 2011
  8. Tim Chase
    Replies:
    2
    Views:
    290
    Miki Tebeka
    Dec 2, 2011
Loading...