Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preserve timestamp with Paramiko

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?

Original file:

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

Uploaded file:

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.

like image 932
Jim Avatar asked Mar 31 '15 08:03

Jim


1 Answers

Paramiko does not support that.

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


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)

Similarly for downloads.

like image 99
Martin Prikryl Avatar answered Oct 18 '22 14:10

Martin Prikryl