title: Matching directory paths with a value in python
body: <p>I have this code where I search for a value (inputCategory), which often is in the end of a path (eg <code>/blah/blah/&lt;category/blah</code> or sometimes <code>/blah/blah/&lt;category&gt;</code></p>
  
  <pre><code>def category_search(inputDirectory, inputCategory, root):
      categorySearch = os.path.split(os.path.normpath(inputDirectory)) 
      if categorySearch[1] == inputName:
          Logger.info("SEARCH: Files appear to be in their own directory")
          categorySearch2 = os.path.split(os.path.normpath(categorySearch[0]))
          if categorySearch2[1] == twoCategory or categorySearch2[1] == oneCategory:
              if not inputCategory:
              Logger.info("SEARCH: Determined Category to be: %s", categorySearch2[1])
                  inputCategory = categorySearch2[1]
          elif not inputCategory:
              Logger.error("SEARCH: Could not identify category from the directory structure.")
              sys.exit(-1)
          else:
              pass
  
      elif categorySearch[1] == twoCategory or categorySearch[1] == oneCategory:
          if os.path.isdir(os.path.join(inputDirectory, inputName)):
              inputDirectory = os.path.join(inputDirectory, inputName)
          else:
              Logger.info("SEARCH: The directory passed is the root directory for category %s", categorySearch[1])
              Logger.info("SEARCH: We will try and determine which files to process, individually")
              root = 1
          if not inputCategory:
              Logger.info("SEARCH: Determined Category to be: %s", categorySearch[1])
              inputCategory = categorySearch[1]
      elif not inputCategory:
          Logger.error("SEARCH: Could not identify category from the directory structure")
          sys.exit(-1)
      else:
          Logger.info("SEARCH: The directory passed does not appear to include a category")
          root = 1
      return inputDirectory, inputCategory, root 
  </code></pre>
  
  <p>But the thing is the code isn't very dynamic and can be thrown off for example if I get a path like <code>/blah/blah/&lt;category&gt;/blah/blah</code></p>
  
  <p>I'm looking for input on how to optimize this code, catch situations like above and perhaps more?</p>
  
  <p>I'm pretty new at python so be gentle :) Thanks for help!</p>