logfile = open(logfilename, 'w')
while tsk.poll() is None:
    line = tsk.stdout.readline()
logfile.write(line)
Merging a Python script's subprocess' stdout and stderr while keeping them distinguishable