开发者

NameError: name 'buffer' is not defined with Ant Based framework batch file

开发者 https://www.devze.com 2023-01-30 11:39 出处:网络
I\'m using a python script to execute an Ant based framework batch file(Helium.bat) subprocess.Popen(\'hlm \'+commands, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

I'm using a python script to execute an Ant based framework batch file(Helium.bat)

subprocess.Popen('hlm '+commands, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

However the script will always stop and display the following error when it executes the .bat file:

import codecs
  File "C:\Python25\lib\codecs.py", line 1007, in <module>
    strict_errors = lookup_error("strict")
  File "C:\Python25\lib\codecs.py", line 1007, in <module>
    strict_errors = lookup_error("strict")
  File "C:\Python25\lib\encodings\__init__.py", line 31, in <module>
    import codecs, types
  File "C:\Python25\lib\types.py", line 36, i开发者_如何学Gon <module>
    BufferType = buffer
NameError: name 'buffer' is not defined

If I execute the .bat directly on command line, there will not be any issue.


I think at least part of the problem is how you're executing the batch file. Give this a try:

# execute the batch file as a separate process and echo its output
Popen_kwargs = { 'stdout': subprocess.PIPE, 'stderr': subprocess.STDOUT,
                 'universal_newlines': True }
with subprocess.Popen('hlm '+commands, **Popen_kwargs).stdout as output:
    for line in output:
        print line,

This pass different arguments to Popen -- the difference are this version removes the shell=True which isn't needed on a batch file, sets stderr=subprocess.STDOUT which redirects stdout to the same place stdout is going to to avoid missing any error messages, and adds a universal_newlines=True to make the output more readable.

Another difference is it reads and prints the output from the Popen process which will effectively make the Python script running the batch file wait until it's finished executing before continuing on -- which I suspect is important.

0

精彩评论

暂无评论...
验证码 换一张
取 消