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

Randy Waters rwaters@csmbid.com
Thu, 9 Aug 2001 12:58:14 -0500


Thanks a lot for the code. It works very well and I will put it in my little
Python bag of tricks for future reference! However, I finally did get the
results I wanted from ftplib .retrlines after playing around with the output
from 'LIST'. This snippet of code lets me detect whether my NT files are
directories or not so that I can do a chdir via ftplib and copy the files in
subdirectories. Thanks again for your help, Israel and Daniel.

--------

#!/usr/bin/python

import os
from ftplib import FTP

session = FTP()
session.connect('someserver.somedomain.com', port=2188)
session.login()

rootList = []
dirList = []
session.retrlines('LIST', rootList.append)

for i in range (len(rootList)):
  if rootList[i][0] == 'd':
    dirList.append(rootList[i])

for j in range (len(dirList)):
  print dirList[j]

session.close()

-----Original Message-----
From: Israel Evans [mailto:israel@lith.com]
Sent: Wednesday, August 08, 2001 12:50 PM
To: 'Daniel Coughlin'; rwaters@csmbid.com
Cc: tutor@python.org
Subject: RE: [Tutor] Using os.path.walk across platforms (fwd)


whoops.  It think I just sent a blank email out.   sorry, trigger finger
overload...


I don't know if this will work on the nt to unix, I haven't tested it
because I don't have access to either one right yet,  but I just re3cently
did something that used all of the generic os stuff to print out a list of
directories and files.  This could be tweaked to do the same thing for you,
I think..


Anyway here it is...  let me know If I'm on crack or anything...
-------------------------------------------------


#! e:\ehold\python21

import os, os.path

def dircont(rootdir=os.getcwd()):
    """Types out a list of directories and files in specified directory
    """

    dirlist = []
    filelist = []

    for item in os.listdir(rootdir):
	itempath = os.path.join(rootdir, item)
	if os.path.isdir(itempath):
	    dirlist.append(item)
	if os.path.isfile(itempath):
	    filelist.append(item)
    print 'Root Directory: ', rootdir
    print '\nFiles:'
    for item in filelist:
	print '\t',item
    print '\nDirectories:'
    for item in dirlist:
	print '\t',item

if __name__ == '__main__':
    import os, os.path
    rootdir = raw_input('directory to search:>')
    dircont(rootdir)
    raw_input('press any key to end')

----------------------------------------------------------


-----Original Message-----
From: Daniel Coughlin [mailto:kauphlyn@speakeasy.org]
Sent: Tuesday, August 07, 2001 7:05 PM
To: rwaters@csmbid.com
Cc: tutor@python.org
Subject: Re: [Tutor] Using os.path.walk across platforms (fwd)


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



_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor