【发布时间】:2017-01-11 01:41:51
【问题描述】:
我尝试了许多策略来重新组织从我的查询中收到的输出,但我无法格式化收到的输出。我目前正在尝试使用“diff”命令比较两个文件,但它会将所有差异发送回去。我试图通过将其设置为“diff -w”来重新格式化它,但 Popen 似乎无法同时识别这两个命令。基本上我正在尝试比较两个 Linux 文件并能够格式化返回的输出,以便用户可以轻松查看正在进行的更改。
#!/usr/bin/env python3
import subprocess
if(input("If you're happy with the changes say yes:") == "yes"):
test = subprocess.Popen(['diff','-w','/file1','/file2'],stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
output = test.communicate()[0]
print(output)
【问题讨论】:
-
定义“混合在一起”?使用
subprocess运行命令并没有什么特别之处;您得到的结果与diff输出的结果完全相同。
标签: python linux python-3.x subprocess