How To Use Subprocess.Popen With Built-in Command On Windows
In my old python script, I use the following code to show the result for Windows cmd command: print(os.popen('dir c:\\').read()) As the python 2.7 document said os.popen is obsole
Solution 1:
You should use call subprocess.Popen with shell=True as below:
import subprocess
result = subprocess.Popen("dir c:", shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output,error = result.communicate()
print (output)
Solution 2:
This works in Python 3.7:
from subprocess import Popen, PIPE
args = ["echo", "realtime abc"]
p = Popen(args, stdout=PIPE, stderr=PIPE, shell=True, text=True)
for line in p.stdout:
print("O=:", line)
.
Output:
O=: "realtime abc"
Post a Comment for "How To Use Subprocess.Popen With Built-in Command On Windows"