Hi All,
I have few questions
1. how to get the process handler?
2. how to keep the process open throughout the entire execution of the script
3. while reading an output from the process, it should not terminate the process, since it should able to get the input again
how to achieve these, would really helps.
Regards,
Maiya
Hi All,
#!/usr/bin/env python3
import subprocess
proc = subprocess.Popen(['ls'], stdin = subprocess.PIPE, stdout = subprocess.PIPE)
proc.stdin.write('\n'.encode())
while True:
output = proc.stdout.readline()
if type(output) is str:
output = output
else:
output = output.decode('utf-8', 'ignore')
if output == '':
break
print(output)
proc.stdin.write('ls -l'.encode())
proc.stdin.write('\n'.encode())
while True:
output = proc.stdout.readline()
if type(output) is str:
output = output
else:
output = output.decode('utf-8', 'ignore')
if output == '':
break
print(output)
proc.wait()
Here I would like to write a command to previously opened process and read the output from it. However for the command which I have fired ('ls'), could able to read output, but then for the second command ('ls -l'), could not able to read any output from.
How to achieve this, while reading output from the process it should not terminate (which I could able to succeed), and process should able to accept the second set of command (which I have fired it in second ) and return the output (since I have list of commands, will be firing one after the other)
In the end how ever doing proc.wait() to terminate the process (will do once all commands are fired and read the output from it)
Much appreciable for any suggestions on this. Thanks a lot.
Regards,
Maiya