#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    103
    Rep Power
    11

    Searching sub directories in a path


    Greets,

    I managed to use bits of code that I found the boards to make a simple search tool to help me determine if I already have a given file(mp3's,flash movies,etc).My problem is searching within sub directories in the path.I can identify the subs with the use of "isdir",but I was wondering how to also search within those subs,and display the results.Also,I was wondering if there was a way to ignore the text case,instead of converting the text to all upper/lower case.I tried using the re module and ignorecase,but I kept getting errors when I ran it.Here's the code:

    Code:
    import os
    
    exit = ('quit', 'exit', 'q', 'e', '0')
    
    
    while True: 
        try: 
        
            path = raw_input('\nDirectory to search: ')
            path2 = path.lower()
            if path2 in (exit): 
                print 'Good Bye'
                break
            
            string = raw_input('String to search for: ')
            string2 = string.lower()
            if string2 in (exit): 
                print 'Good Bye'
                break
            
            ext = raw_input('Specify the extension of files to examine: ')
            ext2 = ext.lower()
            if ext2 in (exit): 
                print 'Good Bye'
                break
    
            count = 0 #total number of objects
            count1 = 0 #number of files
            count2 = 0 #files ending with the extension
            direct = 0 #number of sub-directories in path
    
            found = []
            subs = []
    
            for file in os.listdir(path): 
                count = count+1
    
                if os.path.isdir(path+file): 
                    direct = direct+1
                    subs.append(file)
                    
                if os.path.isfile(path+file): 
                    count1 = count1+1
                    file2 = file.lower()
                    
                    if file2.endswith(ext2): 
                        count2 = count2+1
                        
                    if string2 in (path+file2) and file2.endswith(ext2): 
                        found.append(file)
    
                        
            print '\nTotal Objects In Directory: ', count
            print 'Number Of Sub Directories:', direct
            print 'Number Of Files:', count1
            print 'Number Of Files Ending With "'+ext+'": ', count2
            print '\nSub Directories:'
            #print subs
            for file in subs: 
                print file
                        
            print '\n"'+ext+'" files that have "'+string+'" in the name:'
            #print found
            for file in found: 
                print file
                
        except: 
            print '\nError.Try again.'
    It is not important if the glass is half full or half empty.What is important,is who has been drinking from MY glass?!?!?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    84
    Rep Power
    11
    you want to check out the os.walk() function. it traverses through the directory tree, starting at a root directory that you give it, and applying a function (that you specify) to each directory it comes across. that's just from memory, so you'll need to check out the os module in the library documentation.

    as for the converting to upper/lower case, that's probably the easiest way to do it. however, you don't need two variables for each conversion. if you are on a platform that is case insensitive (Windows), then you don't need to keep the original string and can do:

    path = raw_input("Path: ").lower()

    however, if your on a case sensitive platform, just use the lower() function in your if statements:

    Code:
    path = raw_input("Path: ")
    if path.lower() in exit:
        ...
        do stuff
        ...
    hope that helped a bit. the documentation on the walk() function is at the very bottom of this page:

    http://www.python.org/doc/current/lib/os-file-dir.html
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    103
    Rep Power
    11
    Thank you very much rebbit.The os.walk() seems to be exactly what I'm looking for.But since I am by no means a programmer,I was a little confused by one of the notes(which also explains why I kept the converted text as a new variable).Could anyone please explain to me(in knuckle dragger terms) what this note means:
    Note: If you pass a relative pathname, don't change the current working directory between resumptions of walk(). walk() never changes the current directory, and assumes that its caller doesn't either.
    I am using Windows XP with Python 2.3.Thank you in advance for your patience and help.
    It is not important if the glass is half full or half empty.What is important,is who has been drinking from MY glass?!?!?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    12
    You should also definitely check out Jason Orendorff's path module. It's a much more comfortable way of working with paths.

    What the documentation means when it says that the os.walk function never changes current working directory is that the function returns path names which are relative to the same directory. for instance, "local", "include", "local/bin", "share/doc" are all paths relative to the "/usr" directory if the joined paths exist , like: "/usr/local", "/usr/include", etc. If you were to change directory in the middle of a traversal this means that you would get paths relative to a path other than your current working directory. This is not good.
    Last edited by percivall; May 2nd, 2004 at 03:24 AM.
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    103
    Rep Power
    11
    Ahhh,ok thank you for the explanation percivall.But the link you posted seems to be broken.No biggie though,I'm sure I'll be able to find the info via some searching,seeing how you provided Mr.Orendorff's name.Thanks again!
    It is not important if the glass is half full or half empty.What is important,is who has been drinking from MY glass?!?!?

IMN logo majestic logo spyfu logo threadwatch logo seochat tools logo