how to redirect python's subprocess.Call metho

2019-06-01 08:33发布

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])

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"])

how to achieve this?

1条回答
狗以群分
2楼-- · 2019-06-01 09:20

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 accepts the same arguments as the subprocess.Popen constructor.)

查看更多
登录 后发表回答