compare yesterday's date with last_modified
saramirabi opened this issue · 3 comments
Hi,
Would you mind please let me know how I can compare yesterday's date with the last_modified date? Cause I want to download only yesterday's file via FTP.
Really appreciate for any suggestion.
Some little changes applied to source code:
import ftplib
import os
from datetime import date
from dateutil import parser
FTP_HOST = "Remote IP"
FTP_USER = "Username"
FTP_PASS = "Password"
# some utility functions that we gonna need
def get_size_format(n, suffix="B"):
# converts bytes to scaled format (e.g KB, MB, etc.)
for unit in ["", "K", "M", "G", "T", "P"]:
if n < 1024:
return f"{n:.2f}{unit}{suffix}"
n /= 1024
def get_datetime_format(date_time):
# convert to datetime object
date_time = datetime.strptime(date_time, "%Y%m%d%H%M%S")
# convert to human readable date time string
return date_time.strftime("%Y/%m/%d %H:%M:%S")
# initialize FTP session
ftp = ftplib.FTP(FTP_HOST, FTP_USER, FTP_PASS)
# force UTF-8 encoding
ftp.encoding = "utf-8"
# print the welcome message
print(ftp.getwelcome())
# change the current working directory to 'pub' folder and 'maps' subfolder
ftp.cwd("RemoteDirectory")
print("*"*50, "NLST", "*"*50)
print("{:20} {:19}".format("File_Name", "last_modified"))
for file_data in ftp.mlsd():
file_name,meta = file_data
# convert it to human readable format (i.e in 'KB', 'MB', etc)
last_modified = get_datetime_format(meta.get("modify"))
try:
ftp.cwd(file_name)
except Exception as e:
ftp.voidcmd("TYPE I")
last_modified = get_datetime_format(meta.get("modify"))
print(f"{file_name:20} {last_modified:19}")
ftp.retrbinary("RETR " + file_name, open(file_name, 'wb').write)
# quit and close the connection
ftp.quit()
Br,
Sara
Hello Sara,
Sorry for the late response, you need to define a function like this:
def is_yesterday(date_time):
# convert to datetime object
date_time = datetime.strptime(date_time, "%Y%m%d%H%M%S")
today_date = datetime.now()
time_difference = today_date - date_time
# if the time difference between today and `date_time`
# is 1 day, it means it's yesterday's date!
return time_difference.days == 1The above function parses the FTP date string into a Python datetime, and then it compares it with today's datetime and get the number of the days in difference. After that, it simply compares it with 1!
You can call the function that:
if is_yesterday(last_modified):
ftp.retrbinary("RETR " + file_name, open(file_name, 'wb').write)I hope you find this useful, please confirm if it's solves your problem!
Regards,
Rockikz
Hello Sara,
Sorry for the late response, you need to define a function like this:
def is_yesterday(date_time): # convert to datetime object date_time = datetime.strptime(date_time, "%Y%m%d%H%M%S") today_date = datetime.now() time_difference = today_date - date_time # if the time difference between today and `date_time` # is 1 day, it means it's yesterday's date! return time_difference.days == 1The above function parses the FTP date string into a Python datetime, and then it compares it with today's datetime and get the number of the days in difference. After that, it simply compares it with 1!
You can call the function that:if is_yesterday(last_modified): ftp.retrbinary("RETR " + file_name, open(file_name, 'wb').write)I hope you find this useful, please confirm if it's solves your problem!
Regards,
Rockikz
Thanks it works :-)
Awesome!