views:

77

answers:

1

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.

+6  A: 

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?-)

Alex Martelli
+1 for mentioning ftputil, ftplib is also there
Tumbleweed
Thanks, I have been looking for something similar since ever!
iElectric
@iElectric, you're welcome!
Alex Martelli
@Alex, would have been nice if you mentioned why use `ftputil` instead of stdlib's `ftplib` (EG, ease-of-use).
Tshepang
@Tshepang, I quoted the part about it being **high-level**, a **virtual filesystem**, and generating **file-like objects for remote files** -- these, of course, are the core characteristics that make it attractive. Whether one sees this attractiveness as focused on ease-of-use, sheer power, or conciseness, seems totally secondary to me -- and since my answers tend, if anything, to be overly long (unless I pay special care), I typically try to avoid considerations and observations that strike me as "totally secondary".
Alex Martelli
@Alex, In understand your point, but my main concern is that your answer makes it look like ftplib doesn't even exist, because you don't even mention it.A mention does't need to be longer than "stdlib has ftplib but rather use ftputil because it's high-level... blah..."
Tshepang