0

Im an entry level Python developer working on a requirement where I need to merge so many text files in forder A into one file in Folder B. I wrote the below code which is working but its not writing the entire content from files in folder A.

 import os
 a = open(r"C:\temp\datsOutput\New folder\output.txt", "w")
 path = r'C:\temp\dats'
 for filename in os.listdir(path):
     fullPath = path+"\\"+filename
     with open(fullPath,'r',encoding="utf_8_sig", errors="ignore") as ins:
            for line in ins:
                 a.write(line)

the around 6000 lines from the below line are missing.

 ROUT:=  996.6mm
         ANGL:=  95degree
         ORRF:=  =0/0
       TMRREF:=  =0/0
       REPCOU:=  0
    END
    NEW CYLINDER 26 of SUBEQUIPMENT /FA-3101/MAIN_M1_DAVIT
           NAME:=  =805324448/24319
           TYPE:=  CYLI
           LOCK:=  false
          OWNER:=  /FA-3101/MAIN_M1_DAVIT
           PURP:=  unset
            POS:=  W 537.345mm S 46.083mm U 0mm
            ORI:=  Y is S and Z is U
           LEVE:=  0 10
           OBST:=  2
           DIAM:=  76.2mm
           HEIG:=  304.8mm
           ORRF:=  =0/0
         TMRREF:=  =0/0
         REPCOU:=  0
       :PSTATUS:=  unset
    END
    NEW CYLINDER 27 of SUBEQUIPMENT /FA-3101/MAIN_M1_DAVIT
           NAME:=  =805324448/24320
           TYPE:=  CYLI
           LOCK:=  false
          OWNER:=  /FA-3101/MAIN_M1_DAVIT
           PURP:=  unset
            POS:=  W 537.345mm S 46.083mm U 6.125mm
            ORI:=  Y is S and Z is U
           LEVE:=  0 10
           OBST:=  2

for testing purpose the folder A now has only one file which has 204034 lines and the script has written 203799 lines, is it a problem of encoding? I simply cant go any direction from here any small help is greatly appreciated and saves my job, thank you

  • Try to compare both files and check what is coming extra. – Dinesh Pundkar Sep 14 '17 at 04:15
  • Hi Dinesh Thanks for the response the below are missingROUT:= 996.6mm ANGL:= 95degree ORRF:= =0/0 TMRREF:= =0/0 REPCOU:= 0 END NEW CYLINDER 26 of SUBEQUIPMENT /FA-3101/MAIN_M1_DAVIT NAME:= =805324448/24319 TYPE:= CYLI LOCK:= false OWNER:= /FA-3101/MAIN_M1_DAVIT PURP:= unset POS:= W 537.345mm S 46.083mm U 0mm ORI:= Y is S and Z is U LEVE:IN_M1_DAVIT – Chaitanya Vardhan Sep 14 '17 at 04:17
  • Can you please post the missing lines in the question (by editing) so that it is more clear (like spaces and new lines). Also, have you tried using `utf-8`? Does the same issue occur? – SajidSalim Sep 14 '17 at 04:21
  • I upddated the question to reflect the missing lines, I havent tried using UTF-8 will try it now – Chaitanya Vardhan Sep 14 '17 at 04:23
  • When I try UTF-8 i am getting the below error Traceback (most recent call last): File "C:\Users\chaitanyavardhan\Desktop\datsMerge.py", line 8, in a.write(line) File "C:\Python34\lib\encodings\cp1252.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_table)[0] UnicodeEncodeError: 'charmap' codec can't encode character '\ufeff' in position 0: character maps to – Chaitanya Vardhan Sep 14 '17 at 04:26
  • The lines you have posted are getting copied in my case, irrespective of encoding. Do you do a.close() at the end? Also, see this: https://stackoverflow.com/questions/14630288/unicodeencodeerror-charmap-codec-cant-encode-character-maps-to-undefined – SajidSalim Sep 14 '17 at 04:28
  • when i use UTF-8 its not writing any lines into the File B, unlike before which has written 203799 lines into fileB – Chaitanya Vardhan Sep 14 '17 at 04:30
  • no I havent written a.close at the end, was that causing the problem? – Chaitanya Vardhan Sep 14 '17 at 04:33
  • I wrote a.close at the end, still the same – Chaitanya Vardhan Sep 14 '17 at 04:40

0 Answers0