0

Good morning,

I'm trying to get the latest file in a folder stored in a ftp. That's why I created this function :

def getDataFromCSV_FTP(fileName, index_row_to_start):
    """
    Create a data frame based on the csv named fileName and located on the FTP, starting to read at the index_row_to_start
    """
    try :
        list_of_files = ftp.nlst(fileName) # all files with following the path with csv format
        latest_file = max(list_of_files, key=os.path.getctime)
        df_from_csv = pd.read_csv(latest_file, skiprows=index_row_to_start, dtype="str")
        return df_from_csv
    except :
        print ("Error while reading the file", fileName)

The function is able to find all files on the FTP (I got the list with all files) but I'm getting an issue when it executes the max(). It's telling me that the file doesn't exist (looks like it's looking on the local path) -> FileNotFoundError: [Errno 2] No such file or directory

I'm new with python and I'm missing something here Please can someone help me to figure out what's going wrong ?

Thank you very much

NickNick
  • 223
  • 1
  • 10
  • 1
    `list_of_files` is a list of _remote_ files. You cannot apply a local `os` function on them... – Jean-François Fabre Nov 02 '18 at 10:06
  • Thanks for the links you sent me. Even if I didn't do exactly the same thing, those helped me. Here is the code I used finally : `list_of_files = [] ftp.dir('-t', pathName, list_of_files.append) latest_file_path="""ftp://"""+config['user']+""":"""+config['password']+"""@"""+config['host']+"""/""" + list_of_files[0][56:] df_from_csv=pd.read_csv(latest_file_path, sep=""",""", skiprows=index_row_to_start, dtype=np.object_)` – NickNick Nov 06 '18 at 15:12

0 Answers0