Question

Is there a way to intercept a C function call on a binary (e.g: write to a file) in Python?

I need to execute the binary on Linux from python and capture the output that it writes to the log file. The log file name is unpredictable.

Was it helpful?

Solution

The way to go is to create a named pipe. If logfile is the name of your logfile:

import os
import subprocess

LOGFILE = 'somefile.log'

if not os.path.exists(LOGFILE):
    os.mkfifo(LOGFILE)
proc = subprocess.Popen([YOUR_PROGRAM])
with open(LOGFILE) as log:
    data = log.read()  # process your data

proc.wait()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top