Python program to copy odd lines of one file to other Last Updated : 06 Aug, 2023 Comments Improve Suggest changes Like Article Like Report Program to read the contents of a file and copy only the content of odd lines into a new file. By identifying whether the line number is odd or even in Python. Examples: Input : Hello World Python Language Output : Hello Python Explanation: The output file has only the odd line form the input fileCopy odd lines of one file to another file in PythonHere we first open the file in read mode from which we have to read the data and open the second file in write mode in which we have to write the data. Now we initiate a for loop to integrate over the input file and check whether the line is odd or not and if the line is odd then copy the line from the file and write it in the output file. Python3 def copy_odd_lines(input_file, output_file): with open(input_file, 'r') as infile, open(output_file, 'w') as outfile: for line_number, line in enumerate(infile, 1): if line_number % 2 != 0: outfile.write(line) # Example usage: input_file_name = 'input.txt' output_file_name = 'output.txt' copy_odd_lines(input_file_name, output_file_name) Output: Hello PythonNote: Keep your input file in the same directory. Python to write specific lines from one file to another fileHere we first open the file in read mode from which we have to read the data and open the second file in write mode in which we have to write the data. Now in input with the input file and output file, we also take a number of the specific lines which we need to copy from the input_file. Python3 def copy_specific_lines(input_file, output_file, line_numbers): with open(input_file, 'r') as infile, open(output_file, 'w') as outfile: for line_number, line in enumerate(infile, 1): if line_number in line_numbers: outfile.write(line) # Example usage: input_file_name = 'input.txt' output_file_name = 'output.txt' # Put the line numbers you want to copy here lines_to_copy = [1, 2] copy_specific_lines(input_file_name, output_file_name, lines_to_copy) Output: Hello PythonNote: Keep your file in the same directory. Comment More infoAdvertise with us Next Article Python program to copy odd lines of one file to other P PranshuAgarwal1 Follow Improve Article Tags : Python Practice Tags : python Similar Reads Python - Copy Files From Subfolders to the Main folder In this article, we will discuss how to copy a file from the subfolder to the main folder. The directory tree that will be used for explanation in this article is as shown below: D:\projects\base | |__subfolder: | \__file.txt | |__main.py | Here we have a folder named "base" in which we have a folde 4 min read How to copy file in Python3? Prerequisites: Shutil When we require a backup of data, we usually make a copy of that file. Python supports file handling and allows users to handle files i.e., to read and write files, along with many other file handling options, to operate on files. Here we will learn how to copy a file using Pyt 2 min read How to compare two text files in python? Comparing two text files in Python involves checking if their contents match or differ. This process helps you identify whether the files are exactly the same or if there are any changes between them.To download the text files used in this article, click hereUsing hash-based comparisonThis method ca 3 min read How to Find the Longest Line from a Text File in Python Finding the longest line from a text file consists of comparing the lengths of each line to determine which one is the longest. This can be done efficiently using various methods in Python. In this article, we will explore three different approaches to Finding the Longest Line from a Text File in Py 3 min read Compare two Files line by line in Python In Python, there are many methods available to this comparison. In this Article, We'll find out how to Compare two different files line by line. Python supports many modules to do so and here we will discuss approaches using its various modules. This article uses two sample files for implementation. 3 min read Like