开发者

redirect stdout from subprocess.check_call to function?

Calling subprocess.check_call() allows to specify a file object for stdout, but before writing the data to a file, I would like to modify them on a line-by-line base.

I currently redirect the output to a temporary file (created by tempfile.TemporaryFile(). After check_call finished, I read that temporary file line-by-line,开发者_开发知识库 do the modifications and write the final output file.

Since the output is large, a pure in-memory-solution is not feasible and I would like to modify the data on-the-fly and write to the final output file directly.

Does anyone know how to achieve that?


def check_call_modify(command, modifier_function, output_file)
    p = subprocess.Popen(command, stdout=subprocess.PIPE)
    for line in p.stdout:
        line = modifier_function(line)
        output_file.write(line)    
    p.wait()
    if p.returncode:
        raise subprocess.CalledProcessError(p.returncode, command)
    return p.returncode

to use it pass a function to modify each line, and the file. Dumb example below will save the result of ls -l in uppercase to listupper.txt:

with open('listupper.txt', 'w') as f:
    check_call_modify(['ls', '-l'], operator.methodcaller('upper'), f)


Python is duck-typed, so you could always wrap your file object before passing it to check_call.

This answer has an example of doing it for write() but, to be thorough, you'd probably also want to wrap writelines().

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜