Python Paramiko SFTP获取文件以及文件时间戳/统计信息

Jac*_*Lim 2 python sftp getfiles paramiko

# create SSHClient instance
ssh = paramiko.SSHClient()

list = []

# AutoAddPolicy automatically adding the hostname and new host key
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.load_system_host_keys()
ssh.connect(hostname, port, username, password)
stdin, stdout, stderr = ssh.exec_command("cd *path*; ls")

for i in stdout:
    list.append(i)

sftp = ssh.open_sftp()

for i in list:
    tempremote = ("*path*" + i).replace('\n', '')
    templocal = ("*path*" + i).replace('\n', '')

    try:
        #Get the file from the remote server to local directory
        sftp.get(tempremote, templocal)
    except Exception as e:
        print(e)
Run Code Online (Sandbox Code Playgroud)

远程服务器文件日期已修改统计:6/10/2018 10:00:17

本地文件日期修改统计:当前日期时间

但是我发现修改后的日期在复制文件后发生了变化。

反正还有将远程文件与文件统计信息一起复制到本地文件吗?

Mar*_*ryl 6

传输文件时,Paramiko确实不会保留时间戳。

os.utime下载后,您必须明确调用。


请注意,pysftp(内部使用Paramiko)支持使用pysftp.Connection.get()method保留时间戳。

您可以重用它们的实现(我简化的代码):

sftpattrs = sftp.stat(tempremote)
os.utime(templocal, (sftpattrs.st_atime, sftpattrs.st_mtime))
Run Code Online (Sandbox Code Playgroud)

对于上传也是如此