I am trying to do some basic file parsing within a directory in Python 3. This code works perfectly in Python 2.7, but I can not figure out what the problem is in Python 3.2.
import sys, os, re
filelist = os.listdir('/Users/sbrown/Desktop/Test')
os.chdir('/Users/sbrown/Desktop/Test')
for file in filelist:
infile = open(file, mode='r')
filestring = infile.read()
infile.close()
pattern = re.compile('exit')
filestring = pattern.sub('so long', filestring)
outfile = open(file, mode='w')
outfile.write(filestring)
outfile.close
exit
This is the error that is thrown back:
Traceback (most recent call last):
File "/Users/bunsen/Desktop/parser.py", line 9, in <module>
filestring = infile.read()
File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0x80 in position 3131: ordinal not in range(128)`
The files I am parsing are all text files. I tried specifying the encoding in the method arguments to utf-8, but that didn't work. Any ideas? Thanks in advance!
If I specify the encoding as utf-8, here is the error that is thrown:
Traceback (most recent call last):
File "/Users/sbrown/Desktop/parser.py", line 9, in <module>
filestring = infile.read()
File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0x80 in position 3131: ordinal not in range(128)`