Showing posts with label python ftp directory file list. Show all posts
Showing posts with label python ftp directory file list. Show all posts

Monday, December 28, 2009

How to get the list of files in a ftp directory

Code

from ftplib import FTP #1
ftp = FTP(host='ftp.bls.gov') #2
ftp.login() #3
ftp.cwd('pub/time.series/ap') #4
print ftp.nlst() #5


How this works, line by line
from ftplib import FTP            #1

This imports FTP from the standard ftplib module

ftp = FTP(host='ftp.bls.gov')     #2

This defines lowercase ftp as a FTP connection to the host

ftp.login()                       #3

This statement logs in.

ftp.cwd('pub/time.series/ap')     #4

this changes the directory used in the ftp site

print ftp.nlst()                  #5

this prints a list of the files in the directory