Retrieving latest files from FTP folder (file name with spaces) in Python

I have a requirement when I need to pull the latest files from an FTP folder, the problem is that the file name has spaces, and the file name has a specific pattern. Below is the code that I executed:

import sys from ftplib import FTP import os import socket import time import pandas as pd import numpy as np from glob import glob import datetime as dt from __future__ import with_statement ftp = FTP('') ftp.login('','') ftp.cwd('') ftp.retrlines('LIST') filematch='*Elig.xlsx' downloaded = [] for filename in ftp.nlst(filematch): fhandle=open(filename, 'wb') print 'Getting ' + filename ftp.retrbinary('RETR '+ filename, fhandle.write) fhandle.close() downloaded.append(filename) ftp.quit() 

I understand that I can add an empty list to the ftp.dir () command, but since the file name has spaces, I can not split it correctly and select the last file of the type that I outlined above.

Any help would be great.

+7
python ftplib
source share
2 answers

You can get the mtime file by sending the MDTM command if the FTP server supports it and sorts the files accordingly on the FTP server.

 def get_newest_files(ftp, limit=None): """Retrieves newest files from the FTP connection. :ftp: The FTP connection to use. :limit: Abort after yielding this amount of files. """ files = [] # Decorate files with mtime. for filename in ftp.nlst(): response = ftp.sendcmd('MDTM {}'.format(filename)) _, mtime = response.split() files.append((mtime, filename)) # Sort files by mtime and break after limit is reached. for index, decorated_filename in enumerate(sorted(files, reverse=True)): if limit is not None and index >= limit: break _, filename = decorated_filename # Undecorate yield filename downloaded = [] # Retrieves the newest file from the FTP server. for filename in get_newest_files(ftp, limit=1): print 'Getting ' + filename with open(filename, 'wb') as file: ftp.retrbinary('RETR '+ filename, file.write) downloaded.append(filename) 
+4
source share

The problem is that the FTP command "LIST" returns text for people whose format depends on the implementation of the FTP server.

Using PyFilesystem (instead of the standard ftplib) and its API will provide a search "walk" API that provides Pythonic structures for lists of files and directories hosted on an FTP server.

http://pyfilesystem2.readthedocs.io/en/latest/index.html

0
source share

All Articles