1

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.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
udit kanotra
  • 328
  • 1
  • 3
  • 17
  • No It's not possible. Please go through the below link. [Check it here](https://stackoverflow.com/questions/3423510/comparing-local-file-with-remote-file) – ashok knv May 27 '19 at 06:54
  • @ashok knv please see the answers below – udit kanotra May 27 '19 at 09:32
  • Possible duplicate of [How to sync only the changed files from the remote directory using pysftp?](https://stackoverflow.com/questions/47459065/how-to-sync-only-the-changed-files-from-the-remote-directory-using-pysftp) – Martin Prikryl May 28 '19 at 08:28

2 Answers2

0

If you want to download new files and those which are not on your local system, use rsync. you can sync your local directory with the remote one like below:

rsync -a ~/dir1 username@remote_host:destination_directory

How use it in python:

import subprocess

args = ["rsync", "-av", "-e", "ssh", "user@server:/tmp/", "/home/local/Desktop/"]
subprocess.call(args)

you can pass --password-file switch and it must point to a file contains ssh passwords, or you can use ssh key.

Siyavash vaez afshar
  • 1,303
  • 10
  • 12
-1
def getFilesList(path):
files = []
for (dirpath, dirnames, filenames) in os.walk(path):
    files.extend(filenames)
    return files
ServerFiles = getFilesList(Srverpath)
LocalFiles = getFilesList(Lclpath) 
fileDiffList = []
for file in ServerFiles: 
    if file in LocalFiles: 
        pass 
    else:
        fileDiffList.append(file) 

We can get the uncommon files by using 2 separate lists. Call getFilesList method twice by passing your server path and local file path. At the end your 'fileDiffList' will have file names

Likhith
  • 14