I am trying to write Python code to read all .txt files from one directory then copy it (based from line) to another .txt file:
import os
import glob
path = '/Users/Documents/*.txt'
f1 = open(os.path.expanduser('/Users/Documents/test.txt'),'w')
for data in glob.glob(path):
with open(data) as script:
for line in script:
script.readline()
if 'Subject: ' in line:
f1.write(line)
My code was working, but it can only copy some text from the files, the rest gives an error message, like:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 1658: ordinal not in range(128)
How can I fix this? Anyone?