使用 Paramiko 保留时间戳 [英] Preserve timestamp with Paramiko

查看:79
本文介绍了使用 Paramiko 保留时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 Paramiko 将文件从一台服务器 SFTP 文件从一台服务器到另一台服务器时,是否有一种方法可以保留时间戳,类似于 Linux 中的 -p 参数?

Is there a way of preserving the timestamp when using Paramiko to SFTP files from one server to another similar to the -p argument in Linux?

原始文件:

jim@vm3634:~$ ls -la
-rwxrwx---    1 jim  admin    2214 Mar 30 17:33 compcip.asc

上传的文件:

sftp> ls -la
-rwxrwx---    1 no-user  no-group    2214 Mar 30 18:49 compcip.asc

上传的文件需要与原始文件具有相同的时间戳.

The uploaded file needs to have the same timestamp as the original.

推荐答案

Paramiko 不支持.

Paramiko does not support that.

您必须明确调用SFTPClient.utime 上传后.

You have to explicitly call the SFTPClient.utime after the upload.

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

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

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

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

local_stat = os.stat(localpath)
times = (local_stat.st_atime, local_stat.st_mtime)

sftp.put(localpath, remotepath)

sftp.utime(remotepath, times)

<小时>

同样适用于下载.

这篇关于使用 Paramiko 保留时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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