English 中文(简体)
次级处理模块未能运行指挥
原标题:Subprocess module fails to run command

I m试图执行谷歌灯。 在我的一组档案上打字,并将结果收集到一个记录档案中。 然而,我未能在分处理模块上安装。 我目前的法典在这里是:

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:Python32libsite.py", line 159
    file=sys.stderr)
        ^ SyntaxError: invalid syntax Traceback (most recent call last):   File "C:UsersmeDocumentsdevprojectsrcverifier.py", line 19, in <module>
    output = subprocess.check_output(cmd)   File "C:Python32libsubprocess.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

如果我用一个更简单的东西来替代 cm子:

cmd=[ C:/WinAVR-20100110/bin/avr-gcc.exe , --version ]

然后,文字按预期运作。

I have also tried to use a single command string instead of a list of strings as cmd, but the result is the same. When debugging the code, I copied the list-of-strings-turned-into-the-command-line-command from the debugger and ran it in the Windows command line, and the command ran as expected.

The Python interpreter running my script is Python 3.2. Any tips are greatly appreciated.

最佳回答

浏览像cpplint.py 只是从非零返回代码中提取的,例如,如果在源文档中发现错误或“倾斜”,就会这样做。

。 请注意,如果指挥部执行返回non-zerowal Code,则提出subprocess.CalledProcessError

可以通过观看<代码>CalledProcessError,如:

try:
    output = subprocess.check_output(cmd)
except subprocess.CalledProcessError as e:
    # ack!  cpplint.py failed... report an error to the user?

www.un.org/Depts/DGACM/index_spanish.htm

<>SyntaxError 看来是其中的关键,很可能是由<代码>C:Python32lib在您的PYTHONPATH(或者说,如果是你目前的工作名录)中产生的。

灰色译员(自1.52-ish)在开始时自动运行<编码> 进口场。 因此,当情况发生时,以及你的手稿执行:

c:/Python27/python.exe C:/users/me/Documents/dev/cpplint.py ...

然后,Python 2.7 口译人员将首先找到C:Python32libsite.py,并试图在:Python27libsite.py上装载这些字句,而不是放在(推定)上。 问题是,Salph 3 s site.py 含有与Salv 2相悖的辛塔克斯,因此,通过<代码>subprocess.check_output而启动的进程与SyntaxError一起失败,甚至有机会操作cpplint,该编码传播。 CalledProcessError

解决办法? Make Python 换言之,确保<条码>C:Python32lib,在PYTHONPATH传译员时,不在搜索过程中。

One way to do this in your case is to set an explicit environment when launching the process, e.g.:

python2_env = {"PYTHONPATH": "path/to/python2/stuff:..."}
output = subprocess.check_output(cmd, env=python2_env)
问题回答

谨请您主持第一期工作。

pipe = subprocess.Popen([cmd, options],stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout, stderr = pipe.communicate()

你们会知道,自呼吁程序以来,错误究竟是什么。 只有在离国法典是非零时,才提出过错误。

我这样做的办法是用下文取代倒数主数(我ite开错误,以获得适当的卷宗):

errorlog = sys.stderr
sys.stderr = open("errorlog.csv","w")
sys.stderr.write("File;Line;Message;Category;Confidence
")
for filename in filenames:
  ProcessFile(filename, _cpplint_state.verbose_level)
_cpplint_state.PrintErrorCounts()
sys.exit(_cpplint_state.error_count > 0)
sys.stdout = errorlog
sys.stderr.close()




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签