Enc*_*ner 26 python diff subprocess pipe
我需要使用带有python 子进程模块的标准UNIX diff命令创建一个diff文件.问题是我必须比较文件和流而不创建tempopary文件.我想过通过os.mkfifo方法使用命名管道,但没有达到任何好结果.请问,你能写一个关于如何解决这个问题的简单例子吗?我试过这样:
fifo = 'pipe'
os.mkfifo(fifo)
op = popen('cat ', fifo)
print >> open(fifo, 'w'), output
os.unlink(fifo)
proc = Popen(['diff', '-u', dumpfile], stdin=op, stdout=PIPE)
Run Code Online (Sandbox Code Playgroud)
但似乎diff没有看到第二个论点.
你也许可以考虑使用difflib python模块(我已经链接到这里的一个例子)并创建一些直接生成和打印diff的东西而不是依赖它diff.difflib中的各种函数方法可以接收字符缓冲区,可以将其处理成各种类型的差异.
或者,您可以构造一个shell管道并像这样使用进程替换
diff <(cat pipe) dumpfile # You compare the output of a process and a physical file without explicitly using a temporary file.
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请查看http://tldp.org/LDP/abs/html/process-sub.html