title: Formatting File Paths
body: <p>I'm new to Python so I may be going about this completely wrong, but I'm having problems getting and changing to the directory of a file. My script takes in multiple file names that can be in any directory.  In my script I need python to change to the directory of the file and then perform some actions.  However, I'm having problems changing directories.</p>
  
  <p>Here is what I've tried so far:</p>
  
  <pre><code>path=os.path.split(&lt;file path&gt;)
  os.chdir(path[0])
  &lt;Do things to file specified by path[1]&gt;
  </code></pre>
  
  <p>The way I've been getting the file path is by dragging from explorer to the command line.  This enters the path name as something like <code>"C:\foo\bar\file_name.txt"</code> .  When I run the first line in the interpreter I get out <code>('C:\\foo\bar','file_name.txt')</code>.  The problem is that for some reason the last backslash isn't automatically escaped so when I run the <code>os.chdir(path[0])</code> line I get errors.</p>
  
  <p>My question is why is the last backslash not being automatically escaped like the others?  How can I manually escape the last backslash?  Is there a better way to get the file's directory and change to it?</p>