I am using this code to get last xlsx file from ftp.
import pandas as pd
import ftplib
ftp = ftplib.FTP('ftpname.com', 'client','mdp')
ftp.retrlines('LIST')
ftp.cwd("/DATA")
names = ftp.nlst()
latest_time = None
latest_name = None
for name in names:
time = ftp.sendcmd("MDTM " name)
if (latest_time is None) or (time > latest_time):
latest_name = name
latest_time = time
print(latest_name)
for the moment I can only get the file name as string, but I couldn't read it as a dataframe or to upload the xlsx file from the server to the local machine.
CodePudding user response:
I don't think this question has anything to do with python specifically: you just need to fetch the file the same way you would fetch it with any other FTP client:
for name in names:
time = ftp.sendcmd("MDTM " name)
if (latest_time is None) or (time > latest_time):
latest_name = name
latest_time = time
with open("myfile.xlsx", "wb") as f:
ftp.retrbinary(f"RETR {latest_name}", f.write)
As to reading the resulting file in to a pandas DF, that's a separate question, but now that you have the file you can do it as you normally would.