Remove all the lines that contain the character `a' in a file and write it to another file

file=open('demo.txt','r') # open file in read mode file should be there in the same folder, other wise you have to enter full path of file location ,for example c:\programs\demo.txt
 
fileout=open('output.txt','a')
#second file in which to write, opened in append mode, if file not exist, it will create file.
 
#line will be added to last of file.
 
for line in file: #for loop
   size=len(line) #no of characters in line
   i=0 #set at 0 for each line
   while(i<size):
      if(line[i]=='a'): # if a found, line will be       written to second file
 
          fileout.write(line) 
          break #come out from while loop
      i=i+1  #increment counter of while
 
Explanation : Program is self Explanatory, comments are written against each line. we are opening a file and opening another file named output.txt in append mode. Append mode is a way of opening a file so that we can write content from the end of existing content.
 
Using for loop to go line by line. size variable to is storing no of characters in a line. then in next loop we are checking each character to find 'a'. each line containing character a will be written to other file. using break to break out of while control, then next line will be checked in file and so on.
 
This way we have output.txt file which have all lines containing character a.