pysftp: How to update last modified date
Asked Answered
E

2

1

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?

Epilate answered 13/8, 2020 at 8:3 Comment(0)
E
1

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()
Epilate answered 18/8, 2020 at 10:15 Comment(0)
C
1

Rename (move) of a file does not change the file's modification time. It changes modification time of the folder.

If you want to change modification time of the file, you have to do it explicitly. pysftp does not have an API for that. But you can use Paramiko SFTPClient.utime. See also pysftp vs. Paramiko.

Crissy answered 16/8, 2020 at 6:39 Comment(1)
Thanks @Martin for your reply. It helped me a lot.Epilate
E
1

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()
Epilate answered 18/8, 2020 at 10:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.