Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 1st, 2004, 07:28 PM
Boceifus's Avatar
Boceifus Boceifus is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 103 Boceifus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 1 h 17 m 20 sec
Reputation Power: 10
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?!?!?

Reply With Quote
  #2  
Old May 2nd, 2004, 01:57 AM
rebbit rebbit is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 84 rebbit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 7 m
Reputation Power: 10
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

Reply With Quote
  #3  
Old May 2nd, 2004, 03:03 AM
Boceifus's Avatar
Boceifus Boceifus is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 103 Boceifus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 1 h 17 m 20 sec
Reputation Power: 10
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:
Quote:
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.

Reply With Quote
  #4  
Old May 2nd, 2004, 03:07 AM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
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.

Reply With Quote
  #5  
Old May 3rd, 2004, 01:30 AM
Boceifus's Avatar
Boceifus Boceifus is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 103 Boceifus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 1 h 17 m 20 sec
Reputation Power: 10
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Searching sub directories in a path

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap