如何从Python脚本调用clang格式

时间:2019-02-18 15:47:49

标签: python python-3.x python-2.7 clang clang-format

我有一个生成C ++文件的Python工具。

为了测试该工具,我进行了一项测试,将生成的文件与预期的输出文件进行比较。

diff = difflib.unified_diff(expectedFile.readlines(), file.readlines(), expectedFilename, filename)

问题是由于格式的缘故,我有所不同。

我可以在预期的输出文件上运行clang格式。 我仍在尝试在调用difflib.unified_diff之前对生成的文件运行clang格式。

有人可以帮助我如何在文件中以Python运行clang格式吗?

非常感谢您!

1 个答案:

答案 0 :(得分:0)

您可以使用Python提供的call命令来调用外部命令。例如,您可以编写如下脚本:

#!/usr/bin/python
import sys
from subprocess import call
lc = ["clang-format","test.c"] # replace 'test.c' with the your filename.
retcode=call(lc)
sys.exit(retcode);