0

I have a python script (v3.8) that gets a file once per day from an FTP Server and I want to add the timestamp of the file's creation date at the server when I save it locally.

My code so far:

from ftplib import FTP
ftp = FTP(host)
ftp.login(username,password) 
ftp.cwd('/')
ftp.retrbinary('RETR ' + 'name_of_file.csv', f.write)

f = open('D:/myFolder/name_of_file.csv','wb') 

ftp.quit()
ftp.close()

With the above code I save a csv copy in "myFolder" every day, but If I don't add the timestamp , each file will be overwritten the Next morning.

Desired output: name_of_file_DDMMYYYY_HH_MM.csv

I_m_Possible
  • 31
  • 2
  • 7
  • 1
    `import time; time.strftime('name_of_file_%d%m%Y_%H_%M.csv')`. See also `help(time.strftime)`. –  Nov 27 '20 at 15:28
  • Thank you for your answer. Can you share the command line so I can understand what I am doing wrong? Still can't get it right. – I_m_Possible Nov 27 '20 at 16:20

1 Answers1

0

You can use time.strftime

import time
from ftplib import FTP

ftp = FTP(host)
ftp.login(username,password)
ftp.cwd('/')

name = time.strftime('name_of_file_%d%m%Y_%H_%M.csv')

with open(f'D:/myFolder/{name}', 'wb') as f:
    ftp.retrbinary('RETR ' + 'name_of_file.csv', f.write)

ftp.quit()
ftp.close()

  • Great!! That worked. But, this saves the time that my script will run. Is there a way to capture the timestamp of the file's creation ? For example , using filezilla I can see that the file was created at 2am. I am running the script at 6am, so I want the 2am timestamp to be saved. So I will know if I have a fresh copy or not. Still, many many thanks! – I_m_Possible Nov 27 '20 at 16:32
  • That's a different question and it would be best if you post it separately. To get you started, see https://stackoverflow.com/questions/29026709/how-to-get-ftp-files-modify-time-using-python-ftplib –  Nov 27 '20 at 16:34
  • will do. Thank you – I_m_Possible Nov 27 '20 at 16:36