Python program to copy odd lines of one file to other

Write a python program to read contents of a file and copy only the content of odd lines into new file.

Examples:

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 .

filter_none

edit
close

play_arrow

link
brightness_4
code

# open file in read mode
fn = open('bcd.txt', 'r')
  
# open other file in write mode
fn1 = open('nfile.txt', 'w')
  
# read the content of the file line by line
cont = fn.readlines()
type(cont)
for i in range(0, len(cont)):
    if(i % 2 ! = 0):
        fn1.write(cont[i])
    else:
        pass
  
# close the file
fn1.close()
  
# open file in read mode
fn1 = open('nfile.txt', 'r')
  
# read the content of the file
cont1 = fn1.read()
  
# print the content of the file
print(cont1)
  
# close all files
fn.close()
fn1.close()

chevron_right


Output : Python
         Is


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.