I got a code on this site to download files from remote directory from the server. Now I want modify this code so it compares the files and lists that are on remote directory and not on local directory. It lists the uncommon files between remote directory and local directory . Is this possible? Please help. Thanks in Advance
import os
import pysftp
import socket
from stat import S_IMODE, S_ISDIR, S_ISREG
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
IP = "192.168.X.X"
myUsername = "user"
port = 22
myPassword = "password"
try:
with pysftp.Connection(host=IP, username=myUsername, password=myPassword, cnopts=cnopts) as sftp:
try:
r=str(socket.gethostbyaddr(IP))
print("connection successful with "+r)
def get_r_portable(sftp, remotedir, localdir, preserve_mtime=False):
for entry in sftp.listdir(remotedir):
remotepath = remotedir + "/" + entry
localpath = os.path.join(localdir, entry)
mode = sftp.stat(remotepath).st_mode
if S_ISDIR(mode):
try:
os.mkdir(localpath, mode=777)
except OSError:
pass
get_r_portable(sftp, remotepath, localpath, preserve_mtime)
elif S_ISREG(mode):
sftp.get(remotepath, localpath, preserve_mtime=preserve_mtime)
remote_path = input("enter the remote_path: ")
local_path = input("enter the local_path: ")
get_r_portable(sftp, remote_path, local_path, preserve_mtime=False)
except socket.herror:
print("Unknown host")
except:
print("connection failed")
Outcome should be the uncommon files that are present in remote directory and not in local directory.