MockWhy
MockWhy

Reputation: 153

Traversing an ftp folder with python

I need to write a python script that traverses a folder on a FTP server.

for file in ftpfolder:

#get it
#do something untoward with it

Snippets and non-wheel-reinvention advice welcome.

Upvotes: 3

Views: 4724

Answers (1)

Alex Martelli
Alex Martelli

Reputation: 881665

ftputil is the third-party module you're looking for:

ftputil is a high-level FTP client library for the Python programming language. ftputil implements a virtual file system for accessing FTP servers, that is, it can generate file-like objects for remote files. The library supports many functions similar to those in the os, os.path and shutil modules.

Note for example the snippet here:

# download some files from the login directory
host = ftputil.FTPHost('ftp.domain.com', 'user', 'secret')
names = host.listdir(host.curdir)
for name in names:
    if host.path.isfile(name):
        host.download(name, name, 'b')        # remote, local, binary mode

ftputil is pure Python, very stable, and very popular on pypi (users rate it 9, which I think is the maximum on pypi's scale). What's not to like?-)

Upvotes: 14

Related Questions