Copy odd lines of one file to another file in Python
def copy_odd_lines(input_file, output_file): with open(input_file, 'r') as f_in, open(output_file, 'w') as f_out: for line_number, line in enumerate(f_in): f_out.write(line)
In this article, we will learn about how to copy odd lines of a text file to another text file in python
First open the file in read mode to read, later open file in write mode to write the data
It copies the odd lines of a file to another file by copying only odd lines of a text to another file and prints the result odd lines.
- It uses with statement to open both the input and output files.
- Uses for loop to loop through each line in the input files.
- Enumerate function is used in this.
The line to the output file exists and is readable, later output file can be created.
Here we use the for loop to integrate over the input files.
In this we have learnt how to copy the odd lines from one to another file in python
Leave a Reply