-2

Below is the python script:

import os
from glob import iglob

rootdir_glob = 'D:/FileZilla Data/New 21112019/Raw Data/New folder/ThaiLand' # Note the added asterisks
file_list = [f for f in iglob('D:/FileZilla Data/New 21112019/Raw Data/New folder/ThaiLand/**/*', recursive=True) if os.path.isfile(f)]
results = 'final.txt'

with open('combined_app', "wb") as wfd: 
    for f in file_list:
        if (f.find('RealLifeApp.txt') != -1):
            print(f)
            data = open(f)
            out = open(results,'a')
            for l in data:
                print(l,file=out)
            data.close()
            out.close()

Below is the error i get.

enter image description here

I have no control over the .txt file in regards to making changes.

Is there any way to fix this error or maybe another way to merge the .txt files.

Thanks!

Yatish
  • 27
  • 1
  • 5
  • Does this answer your question? [UnicodeDecodeError: 'charmap' codec can't decode byte X in position Y: character maps to ](https://stackoverflow.com/questions/9233027/unicodedecodeerror-charmap-codec-cant-decode-byte-x-in-position-y-character) – Simas Joneliunas Nov 22 '19 at 08:03

2 Answers2

0

Replace your with open('combined_app', "wb") as wfd: with

with open('combined.app`, 'wb' , encoding=<ENCODING_OF_THE_FILE>) as wfd :

It seems that Python can't decide what is the current file encoding of your file here.

cocool97
  • 1,201
  • 1
  • 10
  • 22
  • How do i determine whats the encoding of the file? Eventually i will be reading these .txt files from a server. – Yatish Nov 22 '19 at 10:17
0

Try adding encoding="utf-8" as a parameter when opening the file.

with open('combined_app', "wb", encoding="utf-8") as wfd:
   "Do something"
  • While your fix is the right one, you're applying it to the wrong file. The error is coming from reading the `data` file, so you need to put the `encoding="utf-8"` argument on the `data = open(f)` line. – Blckknght Nov 22 '19 at 08:15