Skip to content Skip to sidebar Skip to footer

Python Copy File In Local Network (linux -> Linux) And Output

I'm trying to write a script to copy files in my RaspberryPi, from my Desktop PC. Here is my code: (a part) print 'start the copy' path_pi = '//192.168.2.2:22/home/pi/Stock/' fi

Solution 1:

shutil.copy2() works with local files. 192.168.2.2:22 suggests that you want to copy files over ssh. You could mount the remote directory (RaspberryPi) onto a local directory on your desktop machine (sshfs) so that shutil.copy2() would work.

If you want to see the output of a command then don't set stdout=PIPE (note: if you set stdout=PIPE then you should read from p.stdout otherwise the process may block forever):

from subprocess import check_call

check_call(['scp', file_pc, file_pi])

scp will print to whatever places your parent Python script prints.

To get the output as a string:

from subprocess importcheck_outputoutput= check_output(['scp', file_pc, file_pi])

Though It looks like scp doesn't print anything by default if the output is redirected.

You could use pexpect to make scp think that it runs in a terminal:

import pipes
import re
import pexpect # $ pip install pexpectdefprogress(locals):
    # extract percentsprint(int(re.search(br'(\d+)%[^%]*$', locals['child'].after).group(1)))

command = "scp %s %s" % tuple(map(pipes.quote, [file_pc, file_pi]))
status = pexpect.run(command, events={r'\d+%': progress}, withexitstatus=1)[1]
print("Exit status %d" % status)

Solution 2:

Do you have SSH enabled? Something like this could help you:

import os
os.system("scp FILE USER@SERVER:PATH")

Post a Comment for "Python Copy File In Local Network (linux -> Linux) And Output"