title: Python escape character
body: <p>I've struggled for hours with this and although I found a solution, I don't like it.
  Is there a built in way to solve this:</p>
  
  <p>You are on Windows with a variable containing a path.
  You are trying to open a file with it, but it contains escape characters that you can't determine until runtime.</p>
  
  <p>If you use 'shutil' and do:
  <code>shutil.copy(file_path, new_file_path)</code></p>
  
  <p>It works fine.</p>
  
  <p>But if you try and use the same path with:</p>
  
  <p><code>f = open(file_path, encoding="utf8")</code></p>
  
  <p>It doesn't work because the '\a' in the path is read as a 'Bell' = 7</p>
  
  <p>I tried doing all of these, but the only thing I've gotten to work is the custom function 'reconstruct_broken_string'.</p>
  
  <pre><code>    file_path = "F:\ScriptsFilePath\addons\import_test.py"
  
      print(sys.getdefaultencoding())
      print()
      print(file_path.replace('\\', r'\\'))
      print( '%r' % (file_path))
      print( r'r"' + "'" + file_path+ "'")
      print(file_path.encode('unicode-escape'))
      print(os.path.normpath(file_path))
      print(repr(file_path))
  
      print()
      print(reconstruct_broken_string(file_path))
  
  
  backslash_map = { '\a': r'\a', '\b': r'\b', '\f': r'\f',
                    '\n': r'\n', '\r': r'\r', '\t': r'\t', '\v': r'\v' }
  def reconstruct_broken_string(s):
      for key, value in backslash_map.items():
          s = s.replace(key, value)
      return s
  </code></pre>
  
  <p>Here is the printout:</p>
  
  <pre><code>utf-8
  
  F:\\ScriptsFilePathddons\\import_test.py
  'F:\\ScriptsFilePath\x07ddons\\import_test.py'
  r"'F:\ScriptsFilePathddons\import_test.py'
  b'F:\\\\ScriptsFilePath\\x07ddons\\\\import_test.py'
  F:\ScriptsFilePathddons\import_test.py
  'F:\\ScriptsFilePath\x07ddons\\import_test.py'
  
  F:\ScriptsFilePath\addons\import_test.py
  </code></pre>
  
  <p>Is there a built in way to do this rather than this function?
  Why does it work with 'shutil' and not 'open'</p>
  
  <p>Thanks</p>