Created
March 25, 2014 13:05
-
-
Save fredrikaverpil/9761419 to your computer and use it in GitHub Desktop.
Find directory based on search string #python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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