Вопрос

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.

Это было полезно?

Решение

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()
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top