pysftp: How to update last modified date

只谈情不闲聊 提交于 2020-08-26 06:58:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!