Python Program to write those lines which have the character ‘p’ from one text file to another text file

Write a python Program to write those lines which have the character ‘p’ from one text file to another text file with practical example.

Program Description:

The program writes only those lines which have the character ‘p’ from one file to another file.

Program logic:

  • Open input file say book.txt in read mode
  • Open output file say story.txt in write mode
  • Read all lines of input file using readlines() function and store it in variable say s.
  • Use for loop to iterate each line of input file “book.txt” and check ‘p’ character present in lines of input file one by one
  • If ‘p’ character found in line, it will write those lines which have the character ’p’ into output file “story.txt”
  • If ‘p’ character is not found, it will skip that line from writing into output file “story.txt”
  • Close input file say “book.txt” using close() function
  • Close output file say “story.txt” using close() function.

Below is implementation code/Source Code:

Below is output of above program

Output file “story.txt” will look like after writing lines of text which have character ‘p’ from input file “book.txt”

Output text file: Story.txt

Below is input text file “book.txt”

Input text file :”book.txt”

You can Also check this too

  1. Program to check whether it is palindrome or not
  2. Program to create random number generator which generate random number between 1 and 6
  3. Program to write user defined function to swap two number and display number before swapping and after swapping
  4. Python Program to calculate arithmetic operation on two number using user defined function
<