简体   繁体   中英

How to get the stdout and stderr for subprocess.call?

I'm using subprocess.call , and I need to get the output for stdout and stderr. How can I do this?

Notice check_output() :

Note Do not use stdout=PIPE or stderr=PIPE with this function. As the pipes are not being read in the current process, the child process may block if it generates enough output to a pipe to fill up the OS pipe buffer.

My call might produce a lot of output, so what would be a safe way to get the output?

Something like this would work:

f = file('output.txt','w')
g = file('err.txt','w')

p = subprocess.Popen(cmd, shell=True, stdout=f, stderr=g)

f.close()
g.close()

创建一个为写入而打开的文件对象,并将其作为sdtoutstderr传递。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM