How to execute a command prompt command from python

Question:

I tried something like this, but with no effect:

command = "cmd.exe"
proc = subprocess.Popen(command, stdin = subprocess.PIPE, stdout = subprocess.PIPE)
proc.stdin.write("dir c:\")
Asked By: Adrian

||

Answers:

You probably want to try something like this:

command = "cmd.exe /C dir C:\"

I don’t think you can pipe into cmd.exe… If you are coming from a unix background, well, cmd.exe has some ugly warts!

EDIT: According to Sven Marnach, you can pipe to cmd.exe. I tried following in a python shell:

>>> import subprocess
>>> proc = subprocess.Popen('cmd.exe', stdin = subprocess.PIPE, stdout = subprocess.PIPE)
>>> stdout, stderr = proc.communicate('dir c:\')
>>> stdout
'Microsoft Windows [Version 6.1.7600]rnCopyright (c) 2009 Microsoft Corporatio
n.  All rights reserved.rnrnC:\Python25>More? '

As you can see, you still have a bit of work to do (only the first line is returned), but you might be able to get this to work…

Answered By: Daren Thomas

how about simply:

import os
os.system('dir c:\')
Answered By: rmflow

Why do you want to call cmd.exe ? cmd.exe is a command line (shell). If you want to change directory, use os.chdir("C:\"). Try not to call external commands if Python can provide it. In fact, most operating system commands are provide through the os module (and sys). I suggest you take a look at os module documentation to see the various methods available.

Answered By: kurumi

Try adding a call to proc.stdin.flush() after writing to the pipe and see if things start behaving more as you expect. Explicitly flushing the pipe means you don’t need to worry about exactly how the buffering is set up.

Also, don’t forget to include a "n" at the end of your command or your child shell will sit there at the prompt waiting for completion of the command entry.

I wrote about using Popen to manipulate an external shell instance in more detail at: Running three commands in the same process with Python

As was the case in that question, this trick can be valuable if you need to maintain shell state across multiple out-of-process invocations on a Windows machine.

Answered By: ncoghlan

Try:

import os

os.popen("Your command here")
Answered By: vezon122

Taking some inspiration from Daren Thomas’s answer (and edit), try this:

proc = subprocess.Popen('dir C:\', shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
out, err = proc.communicate()

out will now contain the text output.

They key nugget here is that the subprocess module already provides you shell integration with shell=True, so you don’t need to call cmd.exe directly.

As a reminder, if you’re in Python 3, this is going to be bytes, so you may want to do out.decode() to convert to a string.

Answered By: gkimsey

Using ‘ and " at the same time works great for me (Windows 10, python 3)

import os
os.system('"some cmd command here"')

for example to open my web browser I can use this:

os.system(r'"C:Program Files (x86)GoogleChromeApplicationchrome.exe"')

(Edit)
for an easier way to open your browser I can use this:

import webbrowser
webbrowser.open('website or leave it alone if you only want to open the 
browser')
Answered By: AndrewMZ

From Python you can do directly using below code

import subprocess
proc = subprocess.check_output('C:WindowsSystem32cmd.exe /k %windir%System32\reg.exe ADD HKLMSOFTWAREMicrosoftWindowsCurrentVersionPoliciesSystem /v EnableLUA /t REG_DWORD /d 0 /f' ,stderr=subprocess.STDOUT,shell=True)
    print(str(proc))

in first parameter just executed User Account setting you may customize with yours.

Answered By: Abhishek Chaubey

It’s very simple. You need just two lines of code with just using the built-in function and also it takes the input and runs forever until you stop it. Also that ‘cmd’ in quotes, leave it and don’t change it. Here is the code:

import os
os.system('cmd')

Now just run this code and see the whole windows command prompt in your python project!

Answered By: Pranav Karthik

Here’s a way to just execute a command line command and get its output using the subprocess module:

import subprocess
# You can put the parts of your command in the list below or just use a string directly.
command_to_execute = ["echo", "Test"]

run = subprocess.run(command_to_execute, capture_output=True)

print(run.stdout) # the output "Test"
print(run.stderr) # the error part of the output

Just don’t forget the capture_output=True argument and you’re fine. Also, you will get the output as a binary string (b"something" in Python), but you can easily convert it using run.stdout.decode().

Answered By: palsch

In Python, you can use CMD commands using these lines :

import os 

os.system("YOUR_COMMAND_HERE") 

Just replace YOUR_COMMAND_HERE with the command you like.

Answered By: X1525
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.