Drop lines containing 't' Easy File Handling Programs · 36 of 38
Write a Python program to copy a file to another file, skipping lines that contain the letter “t”.
Approach Write only lines where "t" not in line.lower(). Solution
Python Reset Copy Try it ▸
with open ( "source.txt" , "w" ) as f : f . write ( "apple \n butter \n cherry \n melon \n tea \n " ) with open ( "source.txt" ) as src , open ( "filtered.txt" , "w" ) as dst : for line in src : if "t" not in line . lower (): dst . write ( line ) print ( open ( "filtered.txt" ) . read ())
Learn the concept New to this topic? Read the lesson, then come back.
More file handling programs ← Program 35 Count consonants in a fileProgram 37 → Words shorter than 5 characters← All 38 File Handling Programs