title: Python: Replacing backslashes to avoid escape sequences in string
body: <p>I´m trying to replace the single backslashes i get within a string with double backslashes, because sometimes the the "backslash+character" combination creates an escape sequence. I have tried various ways (mostly from other stackoverflow questions), but nothing gets me the correct results so far.</p>
  
  <p>Example   <code>s = "\aa, \bb, \cc, \dd"</code></p>
  
  <hr>
  
  <pre><code>string.replace(s,"\\","\\\\")
  </code></pre>
  
  <p>replaces the first a and b with special characters (can´t get pasting the exact result here to work?):</p>
  
  <pre><code>@a,@b,\\cc,\\dd
  </code></pre>
  
  <hr>
  
  <pre><code>print s.encode("string_escape")
  </code></pre>
  
  <p>produces</p>
  
  <pre><code>\x07a,\x08b,\\cc,\\dd
  </code></pre>
  
  <p>(same for "unicode-escape")</p>
  
  <hr>
  
  <p>using this <a href="https://stackoverflow.com/a/2428144">function</a></p>
  
  <pre><code>escape_dict={'\a':r'\a',
             '\b':r'\b',
             '\c':r'\c',
             '\f':r'\f',
             '\n':r'\n',
             '\r':r'\r',
             '\t':r'\t',
             '\v':r'\v',
             '\'':r'\'',
             '\"':r'\"',
             '\0':r'\0',
             '\1':r'\1',
             '\2':r'\2',
             '\3':r'\3',
             '\4':r'\4',
             '\5':r'\5',
             '\6':r'\6',
             '\7':r'\7',
             '\8':r'\8',
             '\9':r'\9'}
  
  def raw(text):
      """Returns a raw string representation of text"""
      new_string=''
      for char in text:
          try: new_string+=escape_dict[char]
          except KeyError: new_string+=char
      return new_string
  </code></pre>
  
  <p>produces</p>
  
  <pre><code>\7a,\bb,\cc,\dd
  </code></pre>
  
  <hr>
  
  <p>and using this <a href="https://stackoverflow.com/a/24519338">function</a></p>
  
  <pre><code>import re
  import codecs
  
  ESCAPE_SEQUENCE_RE = re.compile(r'''
      ( \\U........      # 8-digit hex escapes
      | \\u....          # 4-digit hex escapes
      | \\x..            # 2-digit hex escapes
      | \\[0-7]{1,3}     # Octal escapes
      | \\N\{[^}]+\}     # Unicode characters by name
      | \\[\\'"abfnrtv]  # Single-character escapes
      )''', re.UNICODE | re.VERBOSE)
  
  def decode_escapes(s):
      def decode_match(match):
          return codecs.decode(match.group(0), 'unicode-escape')
  
      return ESCAPE_SEQUENCE_RE.sub(decode_match, s)
  </code></pre>
  
  <p>returns the string with special characters again</p>
  
  <pre><code> @a,@b,\\cc,\\dd
  </code></pre>
  
  <p>The actual strings i need to convert would be something like <code>"GroupA\Group2\Layer1"</code></p>