Skip to content Skip to sidebar Skip to footer

How To Redirect Python's Subprocess.call() Output To File?

I call an exe tool from Python: args = '-i' + inputFile cl = ['info.exe', args] subprocess.call(cl) How would I redirect output from the info.exe to out.txt?

Solution 1:

You can redirect output to file object by specifying it as stdout argument to subprocess.call. Put it in a context manager to write to file safely.

withopen('out.txt', 'w') as f:
    subprocess.call(cl, stdout=f)

Or open the file in wb mode and use subprocess.check_output:

withopen('out.txt', 'wb') as f:
    f.write(subprocess.check_output(cl))

Post a Comment for "How To Redirect Python's Subprocess.call() Output To File?"