live output from subprocess command

后端 未结 16 1375
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 08:16

I\'m using a python script as a driver for a hydrodynamics code. When it comes time to run the simulation, I use subprocess.Popen to run the code, collect the

16条回答
  •  执念已碎
    2020-11-22 09:22

    Why not set stdout directly to sys.stdout? And if you need to output to a log as well, then you can simply override the write method of f.

    import sys
    import subprocess
    
    class SuperFile(open.__class__):
    
        def write(self, data):
            sys.stdout.write(data)
            super(SuperFile, self).write(data)
    
    f = SuperFile("log.txt","w+")       
    process = subprocess.Popen(command, stdout=f, stderr=f)
    

提交回复
热议问题