[Python] printing stdout in realtime from a subprocess that requires stdin

Discussão em 'Python' iniciado por Stack, Abril 17, 2025.

  1. Stack

    Stack Membro Participativo

    This is a follow up to this question, but if I want to pass an argument to stdin to subprocess, how can I get the output in real time? This is what I currently have; I also tried replacing Popen with call from the subprocess module and this just leads to the script hanging.

    from subprocess import Popen, PIPE, STDOUT
    cmd = 'rsync --rsh=ssh -rv --files-from=- thisdir/ servername:folder/'
    p = Popen(cmd.split(), stdout=PIPE, stdin=PIPE, stderr=STDOUT)
    subfolders = '\n'.join(['subfolder1','subfolder2'])
    output = p.communicate(input=subfolders)[0]
    print output


    In the former question where I did not have to pass stdin I was suggested to use p.stdout.readline, there there is no room there to pipe anything to stdin.

    Addendum: This works for the transfer, but I see the output only at the end and I would like to see the details of the transfer while it's happening.

    Continue reading...

Compartilhe esta Página