Search code examples
pythonstdoutpid

Python get script output with now only the pid of file


for example I run a script

os.execv('script.py',('',))

As I read in docs this command starts a script from your current script by taking it's pid and reasigning it to run script.py. So I can get the pid of process. The question is following: After running execv I need to get the stdout of this script, and the only thing I know is the pid of process. Is it possible to perform this with python ? Any suggestions ? I need to use only execv()


Solution

  • Another possible solution redirecting the output to a file.

    import os,sys
    sys.stdout = open("./data.out","w")
    os.dup2(sys.stdout.fileno(), 1)
    os.execv('/usr/bin/python', ['python', './script.py'])