1

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?

Acontz
  • 471
  • 4
  • 11
Adhit
  • 11
  • 1
  • do your files contain non-ascii characters? Use `codecs.open()` then – martianwars Dec 23 '16 at 18:35
  • all my .txt file, its contain bunch of people typing in newsgroup, they usually typing in ascii characters, but i trying your suggestion, still give me the error. – Adhit Dec 23 '16 at 18:44
  • you did mention the `encoding` as `utf-8` right? – martianwars Dec 23 '16 at 18:46
  • Silly question, but did you write (paste) the script in a non- plaintext editor? This is typically what happens then. – Jacob Vlijm Dec 23 '16 at 18:53
  • @martianwars yes i did. – Adhit Dec 23 '16 at 19:28
  • @JacobVlijm i am using eclipse, is that okay? – Adhit Dec 23 '16 at 19:29
  • I never use Eclipse, but could you try pasting it in a plain text editor? Else check this: http://stackoverflow.com/questions/9180981/how-to-support-utf-8-encoding-in-eclipse Normally, you shouldn't have to set encoding in a script. – Jacob Vlijm Dec 23 '16 at 19:35
  • @JacobVlijm i try your suggestion, and its work, i didn't get any error message, i try pasting it in a plain text editor, but still, for total 1000 .txt file, it didn't take all data, only half or quarter from total data (depends on word). btw, thank you! – Adhit Dec 23 '16 at 21:00
  • Great! Looking at your code, you have an indentation error, and script.readline() shouldn't be there (is unnecessary). Would you mind if I make it an answer tomorrow? (Am on mobile currently) – Jacob Vlijm Dec 23 '16 at 21:17
  • @JacobVlijm yes, i don't mind. – Adhit Dec 23 '16 at 21:54

0 Answers0