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 to add empty lines between lines in text.txt document?

The purpose of the code is to add an empty line between lines in text.txt document and write some words in those empty lines.
I tried looping through every line but the file should be in read mode only;

iushnaufihsnuesa
fsuhadnfuisgadnfuigasdf
asfhasndfusaugdf
suhdfnciusgenfuigsaueifcas

This is a sample of text.txt document
how can i implement this on this txt?

f = open("text.txt", 'w+')
for x in f:
f.write("\n Words between spacing")

f.close()

First i tried directly to just make a new line between each line and add couple of stuuf

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

I also thought of first making empty lines between each line and then add some words in the empty spaces but I didn’t figure this out

>Solution :

Ok, for files in the region of 200 lines long you can store the whole file as a list of strings and add lines when re-writing the file:

with open("text.txt", 'r') as f:
    data = [line for line in f]

with open("text.txt", 'w') as f:
    for line in data:
        f.write(line)
        f.write("Words between spacing\n")
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