title: Append Directory on ubuntu using python
body: <p>I want to get the current file's directory, but by <code>os.path.abspath(__file__)</code>, I can only get something like <code>/home/gmarth/Documents/pythonLearning.py</code>, yet I want to remove the file name, left file's path like <code>/home/gmarth/Documents</code>. I achieved this on Windows with the following code:</p>
  
  <pre><code>current = str(os.path.abspath(__file__))
  for itera in range(len(current) - 1, 0, -1):
      if current[itera] == '\\':
          dir = current[0: itera]
          break;
  self._path = dir
  </code></pre>
  
  <p>But on ubuntu, nearly the same code doesn't work:</p>
  
  <pre><code>current = str(os.path.abspath(__file__))
  for itera in range(len(current)-1, 0, -1):
      if current[itera] == '/':       #only changed here
          directory = current[0: itera]
          break;
  self._path = dierctory 
  </code></pre>
  
  <p>I got:</p>
  
  <pre><code>UnboundLocalError: local variable 'directory' referenced before assignment
  </code></pre>
  
  <p>That confused me, I don't have much experiences on ubnuntu, how can I get the similar result like Windows?</p>
  
  <p>P.S. (Don;t know if that matters) On windows I built it a project; while on Ubuntu it's a single <code>.py</code> file.</p>