Python Paramiko SFTP获取文件以及文件时间戳/统计信息 [英] Python Paramiko SFTP get file along with file timestamp/stat

查看:1562
本文介绍了Python Paramiko SFTP获取文件以及文件时间戳/统计信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

Remote Server File Date Modified Stat : 6/10/2018 10:00:17

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

Local File Date Modified Stat : Current datetime

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

But I found that the date modified changed after done copy the file.

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

Is there anyway to copy remote file along with the file stat to the local file too ?

推荐答案

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

Paramiko indeed won't preserve timestamp when transferring files.

您必须在之后显式调用 os.utime 下载.

You have to explicitly call the os.utime after the download.

请注意, pysftp (内部使用Paramiko)支持使用 pysftp.Connection.get()方法.

Note that pysftp (that internally uses Paramiko) supports preserving the timestamp with its pysftp.Connection.get() method.

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

You can reuse their implementation (code simplified by me):

sftpattrs = sftp.stat(tempremote)
os.utime(templocal, (sftpattrs.st_atime, sftpattrs.st_mtime))


类似地上传.

这篇关于Python Paramiko SFTP获取文件以及文件时间戳/统计信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆