Skip to content Skip to sidebar Skip to footer

Read And Append The Specific Content

Subsequent to my previous question what if I want to append line. In that case others block are also specified in output file. Input file file1.txt ##### Xyz * [] Task 112 * [] Cl

Solution 1:

Read all lines from the input file but write only those that start with specified strings:

with open("file1.txt", "rt") as finp:
    with open("file2.txt", "wt") as fout:
        for line in finp.readlines():
            if line.startswith("#####") or line.startswith("* [x]"):
                fout.write(line)

Post a Comment for "Read And Append The Specific Content"