Editor

Python Program to Copy Lines Without the Letter 'a' to Another File

Intermediate · File handling · CBSE class 12 practical list

This program reads a text file and copies every line that does not contain the letter a into a second file, leaving behind the lines that do contain it. It is a CBSE class 12 practical question, usually worded as: remove all the lines that contain the character a in a file and write it to another file.

Program

remove_lines_with_a.py
# Make a sample file to work on (in your practical file, this file already exists)
with open("notes.txt", "w") as f:
f.write("Python is easy to learn\n")
f.write("Hello world\n")
f.write("Data is stored in files\n")
f.write("Logic is the key\n")
f.write("Practice every day\n")
f.write("Test your code often\n")
copied = 0
with open("notes.txt", "r") as source, open("no_a.txt", "w") as target:
for line in source:
if "a" not in line:
target.write(line)
copied += 1
print("Lines copied:", copied)
print("Contents of no_a.txt:")
with open("no_a.txt", "r") as f:
print(f.read(), end="")
Output
Lines copied: 3
Contents of no_a.txt:
Hello world
Logic is the key
Test your code often

How it works

  1. The program writes six sample lines to notes.txt, and three of them contain the letter a.
  2. A single with statement opens both files: notes.txt for reading and no_a.txt for writing.
  3. For each line, "a" not in line is True only when the letter a appears nowhere in it, and those lines are written to the new file unchanged.
  4. Lines read from a file keep their newline at the end, so target.write(line) keeps them on separate lines in the new file.
  5. Finally the program opens no_a.txt again and prints it, so you can see that only the three lines without an a were copied.

Another way to write it

Moving the lines with "a" out of the original file

Moving the lines with "a" out of the original file
# Make a sample file to work on (in your practical file, this file already exists)
with open("notes.txt", "w") as f:
f.write("Python is easy to learn\nHello world\nData is stored in files\nLogic is the key\n")
with open("notes.txt", "r") as f:
lines = f.readlines() # read everything before the file is emptied
keep = [line for line in lines if "a" not in line]
removed = [line for line in lines if "a" in line]
with open("notes.txt", "w") as f:
f.writelines(keep) # the original file loses the lines with "a"
with open("removed.txt", "w") as f:
f.writelines(removed) # and they are saved in another file
print("Kept in notes.txt:", [line.strip() for line in keep])
print("Moved to removed.txt:", [line.strip() for line in removed])
Output
Kept in notes.txt: ['Hello world', 'Logic is the key']
Moved to removed.txt: ['Python is easy to learn', 'Data is stored in files']

If your question says remove, this version takes the lines containing a out of notes.txt itself and saves them in removed.txt. Reading all the lines with readlines() first matters, because opening a file with "w" empties it straight away.

Try it yourself

Add the line "All OK" to the sample file and change the condition to "a" not in line.lower() so that lines with a capital A are left out too.

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises