Skip to content

Instantly share code, notes, and snippets.

@fredrikaverpil
Created March 25, 2014 13:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredrikaverpil/9761419 to your computer and use it in GitHub Desktop.
Save fredrikaverpil/9761419 to your computer and use it in GitHub Desktop.
Find directory based on search string #python
# This searches for a sequence of characters
import os
def checkForDir(searchDirs, partialOrFullDirNameToFind):
for searchDir in searchDirs:
for directory in os.listdir(searchDir):
if partialOrFullDirNameToFind in directory.lower():
foundDirectories.append(searchDir + directory)
return str(foundDirectories[len(foundDirectories)-1])
foundDirectories = [] # This will store any found directories matching the criteria
searchDirs = ['C:/Program Files (x86)/', 'C:/Program Files/'] # List of dirs to check
partialOrFullDirNameToFind = 'djv' # The search string
djv_path = checkForDir(searchDirs, partialOrFullDirNameToFind) + '/bin/djv_view.exe'
print djv_path
# This searches for whole words
import re
def findWholeWord(w):
return re.compile(r'\b({0})\b'.format(w), flags=re.IGNORECASE).search
findWholeWord('seek')('those who seek shall find') # Match object
findWholeWord('word')('swordsmith') # None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment