[Tutor] Using os.path.walk across platforms (fwd)

Daniel Coughlin kauphlyn@speakeasy.org
Tue, 7 Aug 2001 19:04:59 -0700 (PDT)


Hello,

I wrote a function that might do the trick for you. I am not familiar enough
with the os.path.walk function to know exactly what you are wanting, but I
added a new function and rewrote a couple of lines in your script, which
returns a list of all the directories in the present working directory. This
will
work for a script running on unix connecting to an NT filesystem (at least
it worked on mine ;-) . It did not work going from NT to Unix. You might
need to tweak the regular expression to get it exact.

I am sure this can be improved upon however.

#!/usr/bin/python

import os, string, re
from ftplib import FTP

def FilterDirectories(listing):
    templist = []
    dirlist = []
    for item in listing:
        if item[0] == 'd':
            templist.append(re.split(':',item))
    for item in templist:
        dirlist.append(string.strip(re.sub('\d*','',item[1])))
    for item in dirlist:
        print item + ' is a directory..'
    return dirlist


def GetFiles():
    listings = [] #going to be a list of retrlines
    session = FTP()
    session.connect('myhost', port=21)
    session.login(user=USERNAME, passwd='*****')
    #os.path.walk(session.retrlines('LIST'),TheList,None)
    session.cwd('Music') # delete this line
    session.retrlines('LIST', listings.append)
    FilterDirectories(listings)
    session.close()


GetFiles()
-----------------------------------------------
The output on my system is :

Bell and Sebastian is a directory..
cocteau twins is a directory..
rachels is a directory..
Radiohead is a directory..
Sly & The Family Stone is a directory..
Sonic Youth is a directory..
Talking Heads is a directory..

(These are all, in fact, directories!)

Then I suppose you could use the dirlist to loop through each of the
directories using chdir.

Hope this helps.

Daniel

PS
Hooray for Danny Yoo!!!

>
>---------- Forwarded message ----------
>Date: Tue, 07 Aug 2001 14:17:34 -0500
>From: Randy Waters <rwaters@csmbid.com>
>To: "tutor@python.org" <tutor@python.org>
>Subject: [Tutor] Using os.path.walk across platforms
>
>Hello. I have an interesting little challenge. I need to copy files from
>an NT server to multiple Linux servers. The Python script needs to
>execute on a UNIX server. I am using ftplib and os.path.
>
>My problem is that the standard 'LIST' in (ftplib) retrlines needs to be
>captured to a list so that I can check to see if a entry is a directory
>or not. If I were executing only on UNIX/Linux I could use os.path.walk
>without a problem, but I am trying to "walk" down an NT path from UNIX.
>
>This script:
>
>#!/usr/bin/python
>
>import os, string
>from ftplib import FTP
>
>def TheList(arg, dirname, names):
>         for name in names:
>                 print os.path.join(dirname, name)
>                 if os.path.isdir(name):
>                   print name + ' is a directory...'
>
>def GetFiles():
>   session = FTP()
>   session.connect('myserver.domain.com', port=8282)
>   session.login()
>   os.path.walk(session.retrlines('LIST'), TheList, None)
>   session.close()
>   return
>
>GetFiles()
>-------------------
>Returns this type of information:
>
>dr-xr-xr-x   1 owner    group               0 Dec  7  2000 Co pack
>projects
>dr-xr-xr-x   1 owner    group               0 May 29 14:52 COA
>dr-xr-xr-x   1 owner    group               0 Aug  2  8:45 HANDLING
>-r-xr-xr-x   1 owner    group           12800 Nov  4  1996 INFRZPR2.DOC
>
>-------------------
>
>If I could test the 1st position for 'd' then I would know that I have
>to chdir and read down the tree. Using os.path.isdir does not work
>probably because I am running my script on a UNIX server logged into an
>NT server.
>
>If anyone has any ideas I would appreciate it. Thank you and what a
>great language!
>
>_______________________________________________
>Tutor maillist  -  Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor
>


_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp