I was trying to run cmd.exe
with argument ls
.
I used the below code
import subprocess
subprocess.call(['C:\Windows\System32\cmd.exe', 'ls'])
After executing this cmd.exe
is opening but not taking ls
as input
There are two mistake in your script
ls
not supported in windows use dir
instead /C
parameter needed to run a command Modified script is
>>> import subprocess
>>> subprocess.call(['C:\\windows\\system32\\cmd.exe', '/C', 'dir'])
Note: Use \\
to escape backslash character
I think , it doesn't work with windows , if you want to use linux syntax in windows you have to use cygwin environment. or change command "ls" to "dir" ("dir /w")
If you add argument shell=True, python will use default shell which is available. In this case, python will use Windows cmd. In other word, below code should work:
>>> subprocess.call('dir', shell=True)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.