stdin, stdout and stderr specify the executed program’s standard input, standard output and standard error file handles, respectively. Valid values are PIPE, 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. With the default settings of None, no redirection will occur; the child’s file handles will be inherited from the parent.
您需要传递一个file对象:
引用manual,强调我的:
结合LFJ的回答-建议使用便利函数,您需要使用
shell=True
使通配符(*
)工作:subprocess.call(' '.join(search_command), stdout=open(out_file, 'w'), shell=True)
或者,在使用shell时,也可以使用shell重定向:
subprocess.call("%s > %s" % (' '.join(search_command), out_file), shell=True)
如果要执行shell命令并获得输出,请尝试使用
subprocess.check_output()
。它非常简单,您可以轻松地将输出保存到文件中。在有两个问题:
.fileno()
方法而不是文件名来传递内容*
,但子进程不会调用shell,除非您提出请求。您可以使用glob.glob()
手动展开文件模式。在示例:
相关问题 更多 >
编程相关推荐