我正在尝试对我的一组文件执行 Google 的 cpplint.py 并将结果收集到一个日志文件中。但是,我还没有成功击败子流程模块。我当前的代码在这里:
import os, subprocess
rootdir = "C:/users/me/Documents/dev/"
srcdir = "project/src/"
with open(rootdir+srcdir+"log.txt", mode='w', encoding='utf-8') as logfile:
for subdir, dirs, files in os.walk(rootdir+srcdir):
for file in files:
if file.endswith(".h") or file.endswith(".cpp"):
filewithpath=os.path.join(subdir, file)
cmd=['c:/Python27/python.exe','C:/users/me/Documents/dev/cpplint.py','--filter=-whitespace,-legal,-build/include,-build/header_guard/', filewithpath]
output = subprocess.check_output(cmd)
logfile.write(output.decode('ascii'))
尝试运行上面的代码会引发错误:
File "C:\Python32\lib\site.py", line 159
file=sys.stderr)
^ SyntaxError: invalid syntax Traceback (most recent call last): File "C:\Users\me\Documents\dev\project\src\verifier.py", line 19, in <module>
output = subprocess.check_output(cmd) File "C:\Python32\lib\subprocess.py", line 511, in check_output
raise CalledProcessError(retcode, cmd, output=output) subprocess.CalledProcessError: Command '['c:/Python27/python.exe', 'C:/users/me/Documents/dev/cpplint.py', '--filter=-whitespace,-legal,-build/include,-build/header_guard/', 'C:/users/me/Documents/dev/project/src/aboutdialog.cpp']' returned non-zero exit status 1
如果我用更简单的东西替换 cmd,例如:
cmd=['C:/WinAVR-20100110/bin/avr-gcc.exe','--version']
然后脚本将按预期工作。
我也尝试过使用单个命令字符串而不是字符串列表作为 cmd,但结果是相同的。
调试代码时,我从调试器复制了字符串列表并转换为命令行命令,并在 Windows 命令行中运行它,命令按预期运行。
运行我的脚本的Python解释器是Python 3.2。
非常感谢任何提示。