1

I am trying to open the CMD with python then get it to run a command. After lots of trial and error I tested just opening the cmd line with subprocess code below

subprocess.call('cmd', '/k')

When it brings the cmd prompt up and I try to do a simple command like ll or ls it gives me the 'ls' is not recognized as an internal or external command, operable program or batch file.

is there any way I can open CMD and get it to run a command?

Mark Warren
  • 51
  • 10
  • Um, I'm no windows expert, but I'm pretty sure `ll` and `ls` are unix-isms. I recall they have different names for the windows cmd – juanpa.arrivillaga Dec 17 '18 at 19:10
  • The Unix `ls` equivalent command on Windows is `dir` – Wes Doyle Dec 17 '18 at 19:47
  • So, hi, if possible, see **[this link](https://stackoverflow.com/questions/5486725/how-to-execute-a-command-prompt-command-from-python)**, I´m not sure, but seems to me is the same question. The user asking for **_How to execute a command prompt command from python_** – Io-oI Dec 17 '18 at 20:45

2 Answers2

1

You could do something like this:

1.Get the input

2.Then run the system command

import os
x = input()
os.system(x)
Bitcon
  • 141
  • 1
  • 6
-1

Why would we want to open a command prompt when we can simply run the needed command using the "subprocess" module.

import subprocess
result = []
win_cmd = 'ipconfig'
#shell=True means do not show the command shell
#shell=False means show the command shell
process = subprocess.Popen(win_cmd,
            shell=False,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE )
for line in process.stdout:
    print (line)
result.append(line)
errcode = process.returncode
for line in result:
    print (line)