使用 Paramiko 保留时间戳

编程入门 行业动态 更新时间:2024-10-25 17:26:09
本文介绍了使用 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 保留时间戳

本文发布于:2023-10-17 10:49:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1500682.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   Paramiko

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!