How to launch cmd window, and excute command within the cmd window with Python script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vivawind
    New Member
    • Sep 2010
    • 2

    #1

    How to launch cmd window, and excute command within the cmd window with Python script

    I want to write a Python script to do following:
    1. launch cmd window
    2. execute dos command within cmd window.

    I tried following:

    command = r'start "test" cmd'
    os.system(comma nd)

    => it is able to launch a cmd window with "test" title.

    What is the syntax for not only launch the cmd window, but also able to execute a command, like run a python script.

    I tried to do
    command = r 'start "test" cmd ("dir")'
    - expect it launch the cmd window, and excute "dir" command, doesn't work.
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    Try Pexpect, a Python module written to run programs and watch their output from Python.

    Comment

    • vivawind
      New Member
      • Sep 2010
      • 2

      #3
      Before I spend time studying the Pexpect as you mentioned, wondering if this works well under Window's environment. If not, what will be the solution?

      Comment

      Working...