Python: Listing files in an FTP

Well, I don't know why this had to be harder than it currently is.

ftp = ftplib.FTP(self.get_ftp_host(), username, password)
ftp.cwd("/path/to/files/")
files = list_files(ftp)
print files

def list_files(ftp_connection):
files = []

def dir_callback(line):
bits = line.split()

if ('d' not in bits[0]):
files.append(bits[-1])

ftp_connection.dir(dir_callback)
return files

yWAuj
Great success!

Sources

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog