Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a command prompt command from python

Tags:

python

windows

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:\\") 
like image 393
Adrian Avatar asked Mar 30 '11 13:03

Adrian


People also ask

How do I get Python to recognize a command prompt?

One way to fix the error would be to launch Python from the Command Prompt by passing in the full path to the executable file each time you wanted to run Python. In other words, instead of typing Python you would type something like C:\Users\me\path\to\python.exe .

How do I run a Windows shell command in Python?

The naive approach to run a shell command is by using os. system(): Let's first create a new Python file called shell_cmd.py or any name of your choice. Second, in the Python file, import the os module, which contains the system function that executes shell commands.

Does Python do CMD?

Open Command Prompt and type “python” and hit enter. You will see a python version and now you can run your program there.


1 Answers

how about simply:

import os os.system('dir c:\\') 
like image 188
rmflow Avatar answered Sep 25 '22 06:09

rmflow