在python中使用subprocess.call时如何将stdout重定向到文件? [英] How do I redirect stdout to a file when using subprocess.call in python?

查看:59
本文介绍了在python中使用subprocess.call时如何将stdout重定向到文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从另一个 python 脚本 (A) 调用 python 脚本 (B).

I'm calling a python script (B) from another python script (A).

使用 subprocess.call,如何将 B 的标准输出重定向到指定的文件?

Using subprocess.call, how do I redirect the stdout of B to a file that specify?

我使用的是 python 2.6.1.

I'm using python 2.6.1.

推荐答案

将文件作为 stdout 参数传递给 subprocess.call:

Pass a file as the stdout parameter to subprocess.call:

with open('out-file.txt', 'w') as f:
    subprocess.call(['program'], stdout=f)

这篇关于在python中使用subprocess.call时如何将stdout重定向到文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆