0

I am trying to use Popen to scp a file from my laptop to a device on my network. The process is pretty straight foward...I can get the file to transfer but I cant get the output from the command to display. I am specificlly looking for the percentage complete. Here is what I have:

from subprocess import Popen, STDOUT, PIPE

scp_command = 'scp -i c:<local_key>  <filepath to local file> <user>@<destination_device>:\path'

local_scp_command = Popen(scp_command, text=True, stout=PIPE)
output = local_scp_transfer.communicate

print(output)

I have tried a number of different combinations of stdout and printing the output. I cant even remember all the ways I have tried this. I imagine that there is something kind of easy that I am missing. I am pretty new at programming so even the easy things are compliacted for me.

Thank you so much for all your help!

Aaron
  • 10,133
  • 1
  • 24
  • 40
rogueakula
  • 79
  • 5

1 Answers1

0

Use poll() to determine whether or not the process has finished and read a line:

from subprocess import Popen, STDOUT, PIPE
import shlex

scp_command = 'scp -i c:<local_key>  <filepath to local file> <user>@<destination_device>:\path'

local_scp_command = Popen(shlex.split(scp_command), text=True, stdout=PIPE)

while local_scp_command.poll() is None and line := local_scp_command.stdout.readline():
    print(line)

I added a shlex.split because that's the proper format for Popen.

pigrammer
  • 2,603
  • 1
  • 11
  • 24