1

I am trying to merge and prepend a new line to each txt file using Python. So the goal is to merge all 200 text files into one text file and a new line TEXT, to all 200 files as the first line of data for the text file. Should I prepend and then merge to make life easier?

I tried merging and prepending a new line to the existing txt file but when I open the result file, it deltes all the content in the file and adds a new line TEXT,. Anyway I can fix this?

import glob

read_files = glob.glob("*.txt")

with open("result.txt", "wb") as outfile:
    for f in read_files:

        with open(f, "r") as infile:
            outfile.write(infile.read())

            with open(f,"w+") as infile:
                infile.write("TEXT,"+infile.read())
                with open(f,"r+") as infile:
                    b=infile.read()
                    print b

Actual Result:
TEXT,

Desired Result TEXT, ……………(JUST RANDOM DATA)

joe
  • 53
  • 9
  • 2
    You have 3 different things opened as `infile` at the same time. – Scott Hunter Sep 04 '19 at 14:19
  • Not clear what you want. Do you want `'TEXT'` onece at the beginning of the merged files or do you want `'TEXT'` and the beginning AND *between* the merged files? – wwii Sep 04 '19 at 18:26
  • Possible duplicate of [Python concatenate text files](https://stackoverflow.com/questions/13613336/python-concatenate-text-files), and [Prepend a line to an existing file in Python](https://stackoverflow.com/questions/4454298/prepend-a-line-to-an-existing-file-in-python). – wwii Sep 04 '19 at 18:27

1 Answers1

0

This should do what you want...or be pretty darn close...

import fileinput
import glob

# Creating a list of filenames 
filenames = glob.glob("C:\\your_path_here\\*.txt")

# Open file3 in write mode 
with open('consolidated.txt', 'a') as outfile: 

    # Iterate through list 
    for names in filenames: 

        # Open each file in read mode 
        with open(names) as infile: 

            # read the data from file
            outfile.write(infile.read()) 
            outfile.write('\n\n' + 'TEXT' + '\n\n')

Final Results in Text File:

FName,LName,Address
Jim,Bentz,34 Holloway La.
George,Hororitz,76 Ridge Dr.
Eric,Schimtz,11 Main St.

TEXT

FName,LName,Address
Jim,Bentz,34 Holloway La.
George,Hororitz,76 Ridge Dr.
Eric,Schimtz,11 Main St.

TEXT

FName,LName,Address
Jim,Bentz,34 Holloway La.
George,Hororitz,76 Ridge Dr.
Eric,Schimtz,11 Main St.

TEXT

FName,LName,Address
Jim,Bentz,34 Holloway La.
George,Hororitz,76 Ridge Dr.
Eric,Schimtz,11 Main St.

TEXT

FName,LName,Address
Jim,Bentz,34 Holloway La.
George,Hororitz,76 Ridge Dr.
Eric,Schimtz,11 Main St.

TEXT

There are a few things to be aware of with these kinds of things.

The first step in writing to a file is create the file object by using the built-in Python command "open".

To create and write to a new file, use open with "w" option. The "w" option will delete any previous existing data in the file and write (new contents) to the file. The "a" option will append to a file, rather than write (and overwrite) to a file. The "r" option is used to read a file. See the link below for more info.

https://www.tutorialsteacher.com/python/python-read-write-file

ASH
  • 20,759
  • 19
  • 87
  • 200