使用 windows shell '>' 将输出重定向到文本文件在蟒蛇 [英] redirect output to a text file using windows shell '>' in python

查看:27
本文介绍了使用 windows shell '>' 将输出重定向到文本文件在蟒蛇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 python 脚本中,我正在尝试运行一个打印输出的 Windows 程序.但我想将该输出重定向到文本文件.我试过

In my python script, I am trying to run a windows program that prints output. But I would like to redirect that output to a text file. I tried

     command = 'program' + arg1 + ' > temp.txt'
     subprocess.call(command)

其中 program 是我的程序名称,arg1 是它需要的参数.但它不会将输出重定向到文本文件它只是在屏幕上打印出来.

Where program is my program name and arg1 is argument it takes. but it does not redirect the output to the text file It just prints that on the screen.

谁能帮我怎么做?谢谢!

Can anyone help me how to do this? Thank you!

推荐答案

将文件对象传递给 subprocess.call()stdout 参数:

Pass a file object to the stdout parameter of subprocess.call():

with open('myoutfilename', 'w') as myoutfile:
    subprocess.call(cmd, stdout=myoutfile)

这篇关于使用 windows shell '>' 将输出重定向到文本文件在蟒蛇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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