在使用 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 保留时间戳
发布评论