[Tutor] Getting a List of FTP Dirs?

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sat, 3 Mar 2001 05:00:18 -0800 (PST)


On Fri, 2 Mar 2001, Britt Green wrote:

> How do I tell my program that spice, terradisiene, etc are directories it 
> needs to go into? Since we regularly add and delete directories, I can't 
> create a static file of them. My program needs to take a line like this:
> 
> drwxrwx---   2 restback privftp       512 Mar  2 20:17 spice
> 
> and understand that the last word in it will be a directory it needs to 
> enter.

In cases like this, string.split() is your friend.  *grin*

If you give string.split() a line with the fields separated by a
delimiter, it'll return us a list of those fields.  Here's an example,
edited to fit on a line:

###
>>> string.split('drwxrwx---   2 restback privftp       spice')
['drwxrwx---', '2', 'restback', 'privftp', 'spice']
###

string.split() by default will skip multiple spaces if it needs to, which
is usually what we want.  Also, we can tell if a line given by ftplib's
listing is a directory: there's always a 'd' at the very beginning of the
line.

Writing a function that takes in a line that comes out of the ftplib
listing and extracts out directory names doesn't sound too bad.  Try it,
and if you get stuck, we can help make things less unstuck.

(I think that writing this program might involve a little recursion; can
anyone confirm this?)

Good luck to you.