Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python保存远程目录和本地目录上的comapare文件_Python_Python 3.x_Sftp_Pysftp - Fatal编程技术网

使用python保存远程目录和本地目录上的comapare文件

使用python保存远程目录和本地目录上的comapare文件,python,python-3.x,sftp,pysftp,Python,Python 3.x,Sftp,Pysftp,我在这个网站上得到了一个代码,可以从服务器的远程目录下载文件。现在我想修改这段代码,以便它比较远程目录中的文件和列表,而不是本地目录中的文件和列表。它列出了远程目录和本地目录之间的不常见文件。 这可能吗? 请帮忙。提前谢谢 import os import pysftp import socket from stat import S_IMODE, S_ISDIR, S_ISREG cnopts = pysftp.CnOpts() cnopts.hostkeys = None IP = "1

我在这个网站上得到了一个代码,可以从服务器的远程目录下载文件。现在我想修改这段代码,以便它比较远程目录中的文件和列表,而不是本地目录中的文件和列表。它列出了远程目录和本地目录之间的不常见文件。 这可能吗? 请帮忙。提前谢谢

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")

结果应该是出现在远程目录而不是本地目录中的不常见文件。

如果要下载新文件和不在本地系统中的文件,请使用rsync。您可以将本地目录与远程目录同步,如下所示:

rsync -a ~/dir1 username@remote_host:destination_directory
如何在python中使用它:

import subprocess

args = ["rsync", "-av", "-e", "ssh", "user@server:/tmp/", "/home/local/Desktop/"]
subprocess.call(args)
您可以传递-password文件开关,它必须指向包含ssh密码的文件,或者您可以使用ssh密钥

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) 
我们可以使用两个单独的列表来获得不常见的文件。 通过传递服务器路径和本地文件路径,调用getFileList方法两次。
最后,您的“fileDiffList”将有文件名

它是否适用于python?您可以通过子流程或操作系统模块运行它。如果您愿意,您可以提供一个示例吗?当然,编辑了前面的答案并添加了简单的示例。我尝试过,但一直失败。如果可以的话,你能将上面的r同步代码应用到我的代码中吗?谢谢,不可能。请浏览下面的链接@ashok knv请查看以下答案