Frage

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.

War es hilfreich?

Lösung

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()
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top