-2

I have an issue, where I iterate through the folder and would like to merge files containing particular name. I have files like 1a_USR02.txt, 1b_USR02.txt and 1a_USR06, 1b_USR06. However when I use the following code, the final file FINAL_USR02 or FINAL_USR06 contains only the second file (1b_USR02 or 1b_UR06). Any ideas?

import os
import shutil


cwd = os.getcwd()

directory = (cwd + '\\FINAL' + '\\')

delheadfiles = ['UST04', 'USR02', 'USR06','1251', 'AGRS', 'TEXTS',\
             'USERS', 'FLAGS', 'DEVACCESS', 'USERNAME', 'TSTC', 'TSTCT']

for delheadfile in delheadfiles:   
    for file in os.listdir(directory):
        if file.endswith(delheadfile + ".txt"):
            table04 = (directory + 'FINAL_' + delheadfile + '.txt')
            with open(directory + file, 'rb') as readfile:
                if file.endswith(delheadfile + ".txt"):
                     with open(table04, 'wb') as outfile:                   
                        shutil.copyfileobj(readfile, outfile)
user9799161
  • 123
  • 2
  • 14
  • 1
    You might want to open FINAL using “a” for appending or look into the `seek` method to make sure when you open the file you write at the end and not overwrite its content. – cap Jun 02 '19 at 15:36
  • unfortunately "a"does not work because of the buffer and "a+" together with "w" or "w+" or "wb" creates blank files. – user9799161 Jun 02 '19 at 15:51
  • Please make a [mcve] – wjandrea Jun 02 '19 at 16:38
  • Maybe beside the point, but you have a duplicate `if file.endswith(delheadfile + ".txt")` statement, and you could save an indentation level by merging the two `with` statements. – wjandrea Jun 02 '19 at 16:40

1 Answers1

1

Try this:

import os

files_extensions = ['UST04', 'USR02', 'USR06']

folder_files = os.listdir()

for extension in files_extensions:
    with open('FINAL_' + extension + '.txt', 'a+') as out_file:
        for item in folder_files:
            if item.endswith(extension + '.txt'):
                data = open(item, 'r').read()
                out_file.write(data)
        out_file.close()
Zaraki Kenpachi
  • 5,510
  • 2
  • 15
  • 38