title: python: Change the scripts working directory to the script's own directory
body: <p>I run a python shell from crontab every minute:</p>
  
  <pre><code>* * * * * /home/udi/foo/bar.py
  </code></pre>
  
  <p><code>/home/udi/foo</code> has some necessary subdirectories, like <code>/home/udi/foo/log</code> and <code>/home/udi/foo/config</code>, which <code>/home/udi/foo/bar.py</code> refers to.</p>
  
  <p>The problem is that <code>crontab</code> runs the script from a different working directory, so trying to open <code>./log/bar.log</code> fails.</p>
  
  <p>Is there a nice way to tell the script to change the working directory to the script's own directory? I would fancy a solution that would work for any script location, rather than explicitly telling the script where it is.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <pre><code>os.chdir(os.path.dirname(sys.argv[0]))
  </code></pre>
  
  <p>Was the most compact elegant solution. Thanks for your answers and explanations!</p>