日志进程的STDIN和STDOUT

2024-04-25 05:57:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用subprocess.Popen()运行一个进程,并通过pythonshell与它通信,就像subprocess.Popen通常的行为一样。我想在那份日志旁边写一份日志。在

我该怎么做?在


Tags: 进程subprocesspopenpythonshell
1条回答
网友
1楼 · 发布于 2024-04-25 05:57:19

假设discructurally意味着漫无目的,ranbling意味着所有内容都在同一个文件中,那么下面的片段就是您所要求的。在

区分来源和相互作用的话语式测井

重写其通信方法,如类似问题here

^{1}$

源区判别的话语测井

首先使用StringIO而不是files,然后子类StringIO重写其write方法以打开附加时间戳和源代码的方法。然后编写一个自定义的比较函数,该函数根据时间戳和源进行排序,首先是时间戳,然后是源输入,然后是输出

^{pr2}$

讨论式日志记录

 with open("file.log","wb") as in logfile:
 subprocess.Popen(cmd, shell=True, universal_newlines = True, stdin=logfile, stdout=logfile)

相反的情况如下所示

草书记录

 with open("stdout.txt","wb") as out:
 with open("stderr.txt","wb") as err:
 with open("stdin.txt","wb") as in:
 subprocess.Popen(cmd, shell=True, universal_newlines = True, stdin=in,stdout=out,stderr=err)

相关问题 更多 >