I have the following section to print the ant build info into a file.
rsltFile = open('buildLog.txt', 'w')
p = subprocess.Popen('call ant compile', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout.readlines():
rsltFile.write("%s\n" % line)
print(line)
retval = p.wait()
It worked fine in python 2.6 but it print out some extra character for each line in python 3.2, like this following:
b'Buildfile: C:\\workspace\\VCT2400_Service\\ServiceApplication\\build.xml\r\n'
b'\r\n'
b'init:\r\n'
b'\r\n'
b'clean:\r\n'
b' [mkdir] Created dir: C:\\workspace\\VCT2400_Service\\ServiceApplication\\classes\r\n'
b'\r\n'
b'compile:\r\n'
b' [javac] Compiling 492 source files to C:\\workspace\\VCT2400_Service\\ServiceApplication\\classes\r\n'
Those b' and \r\n' are extra, not expect them there.
Files opened in text mode expect strings to be written into them. You get byte objects from readlines() which are converted to string representation by format % operator.
Open the file in binary mode and dump lines there directly:
rsltFile = open('buildLog.txt', 'wb')
...
rsltFile.write(line + b'\n')
...or, interpret subprocess output as a string:
linestr = line.decode('utf8')
P.S. It looks like adding '\n' is not needed, each line already ends with a newline.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With