user2678074 user2678074 - 7 months ago 10
Python Question

None in redirected stdout

I have this part of following code:

p = subprocess.Popen(['C:/Python27/python.exe', '-m', 'robot', '-d', logs_directory, input_file], stdout=subprocess.PIPE)
for line in iter(p.stdout.readline, ''):
output = sys.stdout.write(line)
#sys.stdout.write(line)
print "\n\n"
print "************************************************"
print output
print "\n\n"
print "************************************************"
print "\n\n"


But 'output' variable in console shows 'none'

************************************************
None



************************************************


What i am doing wrong here?

Answer

sys.stdout.write(line) does not return a value.

You could write your loop like this:

output = ""
for line in iter(p.stdout.readline, ''):
    output += line
    sys.stdout.write(line)

imho you can also remove the sys.stdout.write(line) part. But I don't know what you are meant to do with it so...