Python 3 使用自定义输入和输出的子流程
Python 3 Subprocess using custom input and output
我做了 2 个 类:
Input
实现了 read
方法,以及
Output
实现了 write
方法
我正在尝试调用 shell 命令并捕获输入和输出。这是我的代码:
import subprocess
command = "date"
output = Output()
input = Input()
process = subprocess.Popen(command,
stdout=output, stdin=input, shell=False)
但是当我检查输出实例时,它说它不包含任何数据。
从技术上讲,那些 class 实例可能是无效参数,除非它们继承自文件对象。
根据 documentation(强调我的):
stdin, stdout and stderr specify the executed program’s standard input, standard output and standard error file handles, respectively. Valid values are PIPE, DEVNULL, an existing file descriptor (a positive integer), an existing file object, and None. PIPE indicates that a new pipe to the child should be created. DEVNULL indicates that the special file os.devnull will be used. With the default settings of None, no redirection will occur; the child’s file handles will be inherited from the parent. Additionally, stderr can be STDOUT, which indicates that the stderr data from the applications should be captured into the same file handle as for stdout.
我做了 2 个 类:
Input
实现了 read
方法,以及
Output
实现了 write
方法
我正在尝试调用 shell 命令并捕获输入和输出。这是我的代码:
import subprocess
command = "date"
output = Output()
input = Input()
process = subprocess.Popen(command,
stdout=output, stdin=input, shell=False)
但是当我检查输出实例时,它说它不包含任何数据。
从技术上讲,那些 class 实例可能是无效参数,除非它们继承自文件对象。
根据 documentation(强调我的):
stdin, stdout and stderr specify the executed program’s standard input, standard output and standard error file handles, respectively. Valid values are PIPE, DEVNULL, an existing file descriptor (a positive integer), an existing file object, and None. PIPE indicates that a new pipe to the child should be created. DEVNULL indicates that the special file os.devnull will be used. With the default settings of None, no redirection will occur; the child’s file handles will be inherited from the parent. Additionally, stderr can be STDOUT, which indicates that the stderr data from the applications should be captured into the same file handle as for stdout.