I have a simple script that I use to automate CLI calls to our software (the Moab Workload Manager) in testing, to avoid having to use the '--xml' flag to get xml output and then pipe it through tidy so it's easily readable. It uses a subprocess.Popen call to run the command, then uses str.strip() and str.replace() to do a minor cleanup on the returned xml to make it easy to visually inspect. The code in question is here:
cmdString = "%s --xml" % cmd
cmdList = cmdString.split()
cmdRun = subprocess.Popen(cmdList,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
crOut,crErr = cmdRun.communicate()
xmlOutput = crOut.strip().replace("><",">\n<").replace("\" ","\"\n")
When I run this (I recently upgraded my Python to Python 3.1.2) I now get the following error:
Traceback (most recent call last):
File "/usr/local/bin/xmlcmd", line 50, in <module>
runXMLCmd(getCmd())
File "/usr/local/bin/xmlcmd", line 45, in runXMLCmd
xmlOutput = crOut.strip().replace("><",">\n<")
TypeError: expected an object with the buffer interface
It appears that the communicate() call is returning byte arrays, but in the python interpreter, if I do
dir(bytes) I can still see the strip() and replace() functions. Anybody know how to make this right?
Thanks.