pythonsftpparamikopysftp

pysftp: How to update last modified date


I am trying to move a certain file to another directory after doing some process over it.

Moving the file was easy using Connection.rename

import pysftp
conn = pysftp.Connection(host = 'host', username = 'user', password = 'password')
remote_src = '/dir1/file1.csv'
remote_dest = '/dir2/archive_file1.csv'
conn.rename(remote_src, remote_dest)
conn.close()

But the LastModified date remains same as of original file.
Is there a way to update the LastModified date to current date while renaming?


Solution

  • Thanks to the answer of @MartinPrikryl I was able to finally achieve my purpose.

    pysftp.Connection has a property sftp_client which as per documentation returns the active paramiko.SFTPClient object.
    I used this property to call paramiko.SFTPClient.utime

    import pysftp
    conn = pysftp.Connection(host = 'host', username = 'user', password = 'password')
    remote_src = '/dir1/file1.csv'
    remote_dest = '/dir2/archive_file1.csv'
    conn.rename(remote_src, remote_dest)
    # below is the line I added after renaming the file
    conn.sftp_client.utime(remote_dest, None)
    conn.close()