Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do you append the same text to multiple txt files in python?

Imagine you have some text that you want to split into chunks and send to separate files which are named using l’mahdi’s solution found here

But imagine there is a header at the top of the file:

company:: acme products
department:: sales
floor:: 1

name:: Joe Blogs 
phone:: 123456789
email:: joeblogs@email.com
address:: 123 Main Street
note:: blah blah blah
timestamp::

name:: Josephine Blogs 
phone:: 43217890
email:: josephineblogs@email.com
address:: 123 Main Street
note:: More blah blah
timestamp::

name:: John Smith 
phone:: 23498689
email:: johnsmith@email.com
address:: 1 North Street
note:: Some more blah
timestamp::

How do you append the header to each file so we get the following files?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

# chunk_1.txt
name:: Joe Blogs 
phone:: 123456789
email:: joeblogs@email.com
address:: 123 Main Street
note:: blah blah blah
timestamp:: 2022-07-15 (15h 12m 25s)
company:: acme products
department:: sales
floor:: 1


# chunk_2.txt
name:: Josephine Blogs 
phone:: 43217890
email:: josephineblogs@email.com
address:: 123 Main Street
note:: More blah blah
timestamp:: 2022-07-15 (15h 12m 26s)
company:: acme products
department:: sales
floor:: 1


# chunk_3.txt
name:: John Smith 
phone:: 23498689
email:: johnsmith@email.com
address:: 1 North Street
note:: Some more blah
timestamp:: 2022-07-15 (15h 12m 27s)
company:: acme products
department:: sales
floor:: 1

>Solution :

Just split the header firstly, you can try my solution:

from datetime import datetime
import time
import re

with open('file.txt') as f:
    header, content = f.read().split('\n\n', maxsplit=1)
    for n, chunk in enumerate(content.split('\n\n'), start=1):
        timestamp = datetime.now().strftime('%Y-%m-%d (%Hh %Mm %Ss)')
        chunk = re.sub(r'(timestamp::)', fr'\1 {timestamp}', chunk)
        chunk = chunk.strip() + '\n' + header
        with open(f'chunk_{n}.txt', 'w') as f_out:
            f_out.write(chunk)
        time.sleep(1)

And here is the result:

# chunk_1.txt

name:: Joe Blogs
phone:: 123456789
email:: joeblogs@email.com
address:: 123 Main Street
note:: blah blah blah
timestamp:: 2022-08-07 (13h 10m 08s)
company:: acme products
department:: sales
floor:: 1

# chunk_2.txt

name:: Josephine Blogs
phone:: 43217890
email:: josephineblogs@email.com
address:: 123 Main Street
note:: More blah blah
timestamp:: 2022-08-07 (13h 10m 09s)
company:: acme products
department:: sales
floor:: 1

# chunk_3.txt

name:: John Smith
phone:: 23498689
email:: johnsmith@email.com
address:: 1 North Street
note:: Some more blah
timestamp:: 2022-08-07 (13h 10m 10s)
company:: acme products
department:: sales
floor:: 1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading