我正在尝试在一组文件上执行Google的cpplint.py,并将结果收集到一个日志文件中。然而,我还没有成功使用subprocess模块。这是我的当前代码:
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。 欢迎任何提示。