subprocess.Popen(...)
python3 实现代码: 备注write 要使用bytes 在最后结果转为str 显示时才不会出现 b‘‘ 这样的字节显示
import subprocess obj = subprocess.Popen(["python3"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)obj.stdin.write(bytes(‘print(1)‘,encoding=‘utf-8‘))# obj.stdin.write(‘print 2 \n ‘)# obj.stdin.write(‘print 3 \n ‘)# obj.stdin.write(‘print 4 \n ‘)obj.stdin.close() cmd_out = obj.stdout.read()obj.stdout.close()cmd_error = obj.stderr.read()obj.stderr.close() print(str(cmd_out,encoding=‘utf-8‘))print(cmd_error)
时间: 2024-10-07 20:57:38