如何将python的subprocess.Call方法输出重定向到文件而不是控制台? [英] how to redirect python's subprocess.Call methods output to a file and not console?

查看:101
本文介绍了如何将python的subprocess.Call方法输出重定向到文件而不是控制台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 subprocess 模块中使用 python 的 Call 方法来执行 sqlldr 命令

I am using python's Call method in subprocess module to execute a sqlldr command

from subprocess import call

retcode = call([self.COMMAND, self.CONNECTION_STRING, "control=" +self.CONTROL_FILE, 
            "log="+self.TEMP_LOG_FILE, self.MODE , "data="+loadfile])

当我运行上面的脚本时,sqlldr 命令的输出被打印到控制台,我想重定向到一个文件或忽略它.因为 sqlldr 也会写入指定的日志.

When i run the above script the output of the sqlldr command gets printed to the console which i wan to redirect to a file or ignore it. since sqlldr also writes to the log specified.

我试过这样的事情,将输出重定向到一个文件,但在这一行抛出错误

i tried something like this, to redirect the output to a file, but throwing error at this line

retcode = call([self.COMMAND, self.CONNECTION_STRING, "control=" +self.CONTROL_FILE, 
            "log="+self.TEMP_LOG_FILE, self.MODE , "data="+loadfile, "> discard.log"])

如何实现?

推荐答案

打开一个文件进行写入并将其作为 subprocess.call 的 stdout 关键字参数传递:

Open a file for writing and pass that as the stdout keyword argument of subprocess.call:

with open('stdout.txt', 'wb') as out:
  subprocess.call(['ls', '-l'], stdout=out)

(subprocess.call 接受与 subprocess.Popen 构造函数.)

(subprocess.call accepts the same arguments as the subprocess.Popen constructor.)

这篇关于如何将python的subprocess.Call方法输出重定向到文件而不是控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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