P00dle 0 Junior Poster

Hi

I am ery new to Python. I've only started using it yesterday. Please keep that in mind, as the error might seem simple to you, but I can't understand it.

I am trying to run the following piece of code:

import re

foo = open("PREFIX.txt", "r+")
text = foo.read                             # Read in from file
foo.close()

special = re.compile(r"[etn]")              # Not sure what this does. 

def escape(match):                          # Specifies that any \ has to be escaped
    return "\\" + match.group(0)            

if __name__ == "__main__":
    out = open("WRITE.txt", "w")
    out.write(special.sub(escape, text))         # Write to file
    out.close()

When I check the code using Python's built-in checker, everyhitng checks out fine. However, when I use the console to run the code, the following text is all that appears(It doesnt write to the file):

Traceback (most recent call last):
File "advanced.py", line 14, in (module)
out.write(special.sub(escape, text))
TypeError: expected string of buffer

I don't know if this is vital information: the file I am reading from contains XML, but it is in a text file, so I assumed it would handle everything as text. The goal of the code is to escape the backslashes in the XML.

Thanks