0

I am working on a Python tool to synchronize files between my local machines and a remote server. When I upload a file to the server the modification time property of that file on the server is set to the time of the upload process and not to the mtime of the source file, which I want to preserve. I am using FTP.storbinary() from the Python ftplib to perform the upload. My question: Is there a simple way to preserve the mtime when uploading or to set it after the upload? Thanks.

atarax42
  • 13
  • 8

1 Answers1

0

Short answer: no. The Python ftplib module offers no option to transport the time of the file. Furthermore, the FTP protocol as defined by rfc-959 has no provision to directly get not set the mtime of a file. It may be possible on some servers through SITE commands, but this is server dependant.

If it is possible for you, you should be able to pass a site command with the sendcmd method of a connection object. For example if the server accepts a special SITE SETDATE filename iso-8601-date-string you could use:

resp = ftp.sendcmd(f'SITE SETDATE {file_name} {date-string}')
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • `ftplib.error_perm: 500 'SITE SETDATE' not understood` :-( – atarax42 Oct 11 '21 at 16:54
  • @atarax42 `SITE HELP` or `HELP SITE` could give the list of the SITE commands that the server can understand. – Serge Ballesta Oct 11 '21 at 22:00
  • The following `SITE` commands are recognized: `QUOTA`, The following `SITE` extensions are recognized: `RATIO`, `HELP`, `CHGRP`, `CHMOD` – atarax42 Oct 12 '21 at 08:35
  • @atarax42 Then I am afraid that your server has no support for getting nor setting the mtime of a file... – Serge Ballesta Oct 12 '21 at 08:41
  • But the `FEAT` command revealed the support for the `MFMT` (modify modification time) command that is precisely what I was looking for. I tested it and it worked. Scheme: `ftp.sendcmd(f'MFMT {date_string} {file_name}')` – atarax42 Oct 12 '21 at 09:06