directory search

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blitz2190
    New Member
    • Jan 2008
    • 1

    directory search

    i have been looking everywhere and cant seem to find how to search for a specific directory. i have tried os.path.walk() but had no success what i want to be able to do is search for a directory say c:\program files\program name\desired directory
    then change to that directory using os.chdir()
  • ghostdog74
    Recognized Expert Contributor
    • Apr 2006
    • 511

    #2
    if you know the name of directory beforehand
    Code:
    import os
    mydir=os.path.join("C:\\","mydir_tosearch")
    if os.path.exists(mydir):
        print "found"

    Comment

    • diegososa
      New Member
      • Oct 2007
      • 19

      #3
      What about this:

      Code:
      import os
      
      def searchForDir( dir_looking_for, path_to_search_for, debug = False):
          results=[]
          for root, dirs, files in os.walk( path_to_search_for ):
              if dir_looking_for in dirs:
                  results.append(root)
          if debug:
              if results:
                  print "There is %i results for your query, and they are: " % (len(results),)
                  for item in results:
                      print "\t", item
              else:
                  print "Sorry, there is no directory named '%s' at '%s' ! :("  % (dir_looking_for, path_to_search_for)
          
          return results
      
      
      # Main:
      what  = 'doc'
      where = 'c:\\Python25'
      founds = searchForDir( what, where, 'debug')

      Comment

      Working...