Write a python program to read contents of a file and copy only the content of odd lines into new file.
Input : Hello World Python Language Output : Hello Python Input : Python Language Is Easy Output : Python Is
Approach to the problem
1) Open file name bcd.txt in read mode and assign it to fn.
2) Open file name nfile.txt in write mode and assign it to fn1.
3) Read the content line by line of the file fn and assign it to cont.
4) Access each element from 0 to length of cont.
5) Check if i is not divisible by 2 then write the content in fn1 else pass.
6) Close the file fn1.
7) Now open nfile.txt in read mode and assign it to fn1.
8) Read the content of the file and assign it to cont1.
9) Print the content of the file and then close the file fn and fn1 .
Output : Python Is
- Python - Reading last N lines of a file
- Count number of lines in a text file in Python
- Python - Get number of characters, words, spaces and lines in a file
- Python program to reverse the content of a file and store it in another file
- Python Program to delete a file
- Python Program to merge two files into a third file
- Python Program to Reverse the Content of a File using Stack
- Python program to modify the content of a Binary File
- Python program to Reverse a single line of a text file
- Python program to Count the Number of occurrences of a key-value pair in a text file
- Python program to read file word by word
- Python program to read character by character from a file
- Python | Farthest point on horizontal lines in 2D plane
- Python | How to copy a nested list
- Python | Move or Copy Files and Directories
- Copy a directory recursively using Python (with examples)
- Python | Copy and Paste Images onto other Image using Pillow
- How to read from a file in Python
- Writing to file in Python
- Python append to a file
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.