How to execute a command prompt command from python
Solution 1
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]\r\nCopyright (c) 2009 Microsoft Corporatio
n. All rights reserved.\r\n\r\nC:\\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...
Solution 2
how about simply:
import os
os.system('dir c:\\')
Solution 3
Try:
import os
os.popen("Your command here")
Solution 4
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('"C:\Program Files (x86)\Google\Chrome\Application\chrome.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')
Solution 5
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.
Related videos on Youtube
Adrian
Updated on September 29, 2021Comments
-
Adrian over 2 years
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:\\")
-
Sven Marnach about 13 yearsTry to use
proc.communicate()
, or at leastproc.stdin.flush()
after writing to it.
-
-
Sven Marnach about 13 yearsFor testing, I started Windows XP inside a VirtualBox and tried
echo dir | cmd
-- works fine. So you can pipe tocmd.exe
. -
darkAsPitch about 13 years@Sven Marnach: You are right, it seems to work, I have updated my answer.
-
ncoghlan about 13 yearsAs I noted in my answer, failing to terminate the command with
"\n"
and failing to explicitly flush the pipe are the two prime candidates I can see as to why the poster's code isn't producing anything onproc.stdout
. (I missed that Sven had already mentioned flushing the pipe in the comments) -
Syeda Zunaira over 9 yearsPlease Give some details.
-
Jan-Bert over 6 yearsAfter testing the answer in anaconda with (I)python3 on Windows10 i get an error:
TypeError: a bytes-like object is required, not 'str'
. Can anybody confirm that it don't work on Windows10, python3 or anaconda? -
GeeTransit about 5 years@Jan-Bert Try putting
b
before it like so:b'byte string here'
. This is probably due to this answer's age and that it's using Python 2, not 3. Check out docs.python.org/3/howto/pyporting.html#text-versus-binary-data. -
sattva_venu over 3 yearsI tried this with python 3.7 on windows, getting error as
TypeError: a bytes-like object is required, not 'str'
-
darkAsPitch over 3 years@sattva_venu, in Python 3, you'll have to encode the string.