Python program to copy odd lines of one file to other Last Updated : 11 Jul, 2025 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 P PranshuAgarwal1 Follow Improve Article Tags : Python Practice Tags : python Explore Python FundamentalsPython Introduction 3 min read Input and Output in Python 4 min read Python Variables 6 min read Python Operators 5 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 7 min read Python Functions 8 min read Recursion in Python 6 min read Python Lambda Functions 6 min read Python Data StructuresPython String 6 min read Python Lists 6 min read Python Tuples 6 min read Dictionaries in Python 7 min read Python Sets 10 min read Python Arrays 9 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 6 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 12 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 11 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 6 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library- Tutorial 4 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 7 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 10 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 3 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like