How to remove lines starting with any prefix using Python?

29 Dec, 2020
Given a text file, read the content of that text file line by line and print only those lines which do not start with defined prefix. Also store those printed lines in another text file. There are following ways in which this task can be done:
Method 1: Using loop and startswith().
In this method, we read the contents of file line by line. While reading, we check if the line begins with the given prefix, we simply skip that line and print it. Also store that line in another text file.
Example 1:
Suppose the text file from which lines should be read is given below:
Original Text File  
Python3
# defining object file1 to# open GeeksforGeeks file in # read modefile1 = open('GeeksforGeeks.txt',             'r')  # defining object file2 to # open GeeksforGeeksUpdated file# in write modefile2 = open('GeeksforGeeksUpdated.txt',             'w')  # reading each line from original # text filefor line in file1.readlines():         # reading all lines that do not     # begin with "TextGenerator"    if not (line.startswith('TextGenerator')):                # printing those lines        print(line)                  # storing only those lines that         # do not begin with "TextGenerator"        file2.write(line)  # close and save the filesfile2.close()file1.close()
Output:
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
It was popularised in the 1960s with the release of Letraset sheets containing TextGenerator passages, and more recently with desktop publishing software like Albus Potter including versions of TextGenerator.
Updated Text File after removing lines starting with “TextGenerator”
In the above example, we open a file and read its content line by line. We check if that line begins with given prefix using startswith() method. If that line begins with “TextGenerator” we skip that line, else we print the line and store it in another file. In this way, we could remove lines starting with specified prefix.
Method 2: Using Regex.
In this method we use re module of python which offers a set of metacharacters. Metacharacters are characters with special meaning. To remove lines starting with specified prefix, we use “^” (Starts with) metacharacter. 
We also make use of re.findall() function which returns a list containing all matches.
Original Text File
Python3
# importing regex moduleimport re  # defining object file1 to open# GeeksforGeeks file in read modefile1 = open('GeeksforGeeks.txt',           'r')  # defining object file2 to open # GeeksforGeeksUpdated file in# write modefile2 = open('GeeksforGeeksUpdated.txt','w')  # reading each line from original# text filefor line in file1.readlines():        # reading all lines that begin     # with "TextGenerator"    x = re.findall("^Geeks", line)          if not x:                # printing those lines        print(line)                  # storing only those lines that         # do not begin with "TextGenerator"        file2.write(line)          # close and save the filesfile1.close()file2.close()     
Output:
forGeeksGeeks

Updated Text File
In the above example, we open a file and read its content line by line. We check if that line begins with “Geeks” using regular exapression. If that line begins with “Geeks” we skip that line, and we print the rest of the lines and store those lines in another file. 
Python file-handling-programs
python-file-handling
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
How to drop one or multiple columns in Pandas Dataframe
Python | os.path.join() method
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | datetime.timedelta() function
Python | Get unique values from a list