开发者

how to redirect python's subprocess.Call methods output to a file and not console?

开发者 https://www.devze.com 2023-04-06 20:29 出处:网络
I am using python\'s Call method in subprocess module to execute a sqlldr command from subprocess import call

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

from subprocess import call

retcode = call([s开发者_Python百科elf.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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号