Okay, having played around with this for another day, I found a solution, as follows:
dirLoc='source/folder'
import os
import codecs
files=os.listdir(dirLoc)
for f in files:
if f.endswith('.txt'):
data=codecs.open(dirLoc+'/'+f, 'r+', encoding='utf8')
try:
data1=data.read()
out=codecs.open(dirLoc+'/outPut.txt', 'a+', encoding='utf8')
try:
out.write(data1)
except:
print('file ' +f+ ' failed to write')
except:
print('file '+f+' failed to read')
out.close()
data.close()
The codecs.open
allows me to specify encoding for the write function as well as the read - note that you have to import codecs
in order to use it. The exceptions are there because the encoding is still a problem and the occasional file throws an exception. The try allows me to skip any file that does fail to read or write without failing altogether.