Python: Listing files in an FTP

0 Comments

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

01.ftp = ftplib.FTP(self.get_ftp_host(), username, password)
02.ftp.cwd("/path/to/files/")
03.files = list_files(ftp)
04.print files
05. 
06.def list_files(ftp_connection):
07.  files = []
08. 
09.  def dir_callback(line):
10.    bits = line.split()
11. 
12.    if ('d' not in bits[0]):
13.      files.append(bits[-1])
14. 
15.  ftp_connection.dir(dir_callback)
16.  return files

yWAuj
Great success!

Sources

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