I've seen a lot of various ways to work with pipes in python, however they are too complicated to understand. What i would like is to write something like this:
import os
for cmdoutput_line in os.system('find /'):
print cmdoutput_line
what is simplest way to achieve it without waiting+big-buffering command output? i dont want to wait while command finish, i just want to iterate output in real-time.
In a while statement you can read line by line with subprocess ,
from subprocess import Popen, PIPE, STDOUT
process = Popen('find /', stdout = PIPE, stderr = STDOUT, shell = True)
while True:
line = process.stdout.readline()
if not line: break
print line
from subprocess import Popen, PIPE
def os_system(command):
process = Popen(command, stdout=PIPE, shell=True)
while True:
line = process.stdout.readline()
if not line:
break
yield line
if __name__ == "__main__":
for path in os_system("find /tmp"):
print path
Try this:
import subprocess
sp = subprocess.Popen('find /', shell=True, stdout=subprocess.PIPE)
results = sp.communicate()
print results
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.