This python program will read the contents of one file and copy the content of the odd line into the new file.
Steps to write & execute the program:
- Open the first file in read mode.
- Assign the first file to f1.
- Open the second file in write mode.
- Assign the second file to f2.
- Read the content line by line of the file f2 and assign it to cont.
- Access each element from 0 to length of cont.
- Check if i is not divisible by 2 then write the content in f1 else pass.
- Close the file f1.
- Now open file2.txt in read mode and assign it to f1.
- Read the content of the file and assign it to cont1.
- Print the content of the file and then close the file f2 and f1 .
Python Program To Copy Odd Lines Of One File To Other
# open first file in read mode f1= open('fileone.txt', 'r') open the second file in write mode f2= open('filetwo.txt', 'w') # read the content of the file line by line cont = f1.readlines() type(cont) for i in range(0, len(cont)): if(i % 2 ! = 0): f2.write(cont[i]) else: pass # close the second file f2.close() # open the second file in read mode f2= open('filetwo.txt', 'r') # read the content of the file cont1 = f2.read() # print the content of the file print(cont1) # close all files f1.close() f2.close()