ftplib LIST question

Michael Bacarella mbac at netgraft.com
Tue Aug 6 17:58:36 CEST 2002


In article <MPG.17b98c8bada1b03098968c at mayonews>, Al wrote:
> [..]
> Thanks for the very useful feedback guys!  Now I can really do some 
> damage :-)
> 
> - Al

There's a slightly simpler way..

I just finished writing some regexes to do this over the weekend.
Note, I was only interested in 3 fields. The file name, is it
a directory, and the file size. Shouldn't be too hard to extend.

Gratiuitous code pasting alert:

def parseline(x):
    global Directory_Sequence

    ## first line sent by UNIX list, safely ignored
    if x[0:6] == 'total ':
        return

    ## try NT-style list match
    ## 11-10-01  03:36AM       <DIR>          blahdir
    ## 08-24-01  02:55AM              6739771 blahfile

    mo = re.match('^\d\d-\d\d-\d\d *\d\d:\d\d[AP]M *(<DIR>)? *(\d*) (.*)$',x)
    if mo != None:
        dir,size,name = mo.groups()
        if dir == '<DIR>': dir = 1
        else: dir = 0
        print 'NT match: %d,%s,%s' % (dir,size,name)
        Directory_Sequence.append({'dir':dir,'size':int(size),'name':name})

    ## try ls (UNIX) style list match
    ## drwx------  6 mbac  user      512 Apr 28  2001 foobarbaz
    ## drwxr-sr-x  5 1              1024 Jun 28  2001 pub

    mo = re.match('^(d|-)\S+ +\d* +\S+ +\S+ +(\d+) +\S+ +\S+ +\S+ +(.*)$',x)
    if mo != None:
        dir,size,name = mo.groups()
        if dir == 'd': dir = 1
        else: dir = 0
        #print 'UNIX match: %d,%s,%s' % (dir,size,name)
        try:
            if index(name,'->'):
                return
        except ValueError:
            pass
        Directory_Sequence.append({'dir':dir,'size':int(size),'name':name})


If it matters, released into public domain.

Criticisms welcome. =)

-- 
``I get back to work and the janitor's asleep at the terminal.
I ask him if he wants to work here too, but he likes the ability
to bust in on people when they're in the toilet...''



More information about the Python-list mailing list