问题
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?
回答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.
回答2:
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()
来源:https://stackoverflow.com/questions/63390720/pysftp-how-to-update-last-modified-date