I am trying to run a comparison between the output of a command called by subprocess.check_output(). Since I am running this on Windows I am also getting \r\n‘s in the output (which is a good thing).
Now I want to compare that output from that command with a text file. This fails because the open() does not preserve the \r‘s. Here is what I got so far:
try:
output = subprocess.check_output(paramList, universal_newlines=False,
stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as err:
output = err.output
errorCode = err.returncode
with open(EMCMD_INCORRECT_PARAMS, 'r') as fd_usage:
usageLines = fd_usage.read()
usage = True if usageLines == output else False
Any suggestions to preserve the \r‘s? Thanks!
In python 3, open takes an
newlinesargument to control the behaviour of the newline translator (see the docs). A values of''for this argument disables the newline translation.If you can’t use python 3, file objects opened by
openhave anewlineattribute that contains the original type of newlines, so you can use this to get the original content :Binary mode might also be a solution if you don’t like the idea of post-processing what
readreturns to get the original content of the file.