[Tutor] Using os.path.walk across platforms

Randy Waters rwaters@csmbid.com
Tue, 07 Aug 2001 14:17:34 -0500


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!