Removing lines from text files | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Removing lines from text files

Does anyone have ideas on how to remove a line containing a specific character from a text file using python? Say for example, that I want to remove all lines from a text file that contain the letter 'z'. I tried using readlines() and then removing those lines which contain 'z' from the list but these changes are obviously not reflected on the file itself. Is there another way to do this?

19th Mar 2021, 6:35 AM
Sriya
1 Answer
+ 1
you should readlines(), filter lines to get only lines to be keeping, and then save either in a new file or in initial file... (technically you make a new file in both cases)
19th Mar 2021, 6:37 AM
visph
visph - avatar