How to Copy Odd Lines of Text File to Another File using Python


In this article, we will show you how to copy only odd lines of a text file to another text file using python.

Assume we have taken a text file with the name TextFile.txt consisting of some random text. We need to copy just all the odd lines of a text file into another and print them.

TextFile.txt

Good Morning
This is the Tutorials Point sample File
Consisting of Specific
source codes in Python,Seaborn,Scala
Summary and Explanation
Welcome everyone
Learn with a joy

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the path of the text file.

  • Use the open() function (opens a file and returns a file object as a result) to open the text file in read-only mode by passing the file name, and mode as arguments to it (Here “r” represents read-only mode).

readFile = open(inputFile, "r")
  • Create a variable to store the output file path which consists of only the odd lines from the given input file.

  • Use the open() function (opens a file and returns a file object as a result) to open the output file in write mode by passing the file name, and mode as arguments to it (Here “w” represents write mode).

  • Using the readlines() function (returns a list with each line in the file represented as a list item. To limit the number of lines returned, use the hint argument. No more lines are returned if the total amount of bytes returned exceeds the specified number) to obtain the list of lines of a given input text file.

file.readlines(hint)
  • Traverse in each line of the read text file till the length of the file using the for loop. Use the len() function (The number of items in an object is returned by the len() method) to calculate the length of the read file.

  • Using the if conditional statement, determine whether the index of the read file lines is odd.

  • If the condition is true, then use the write() function (writes a specified text to a file. The provided text will be inserted depending on the file mode and stream position) to write the read file line into the output file.

  • Print the odd line from the given input file.

  • Close the write file (output file) with the close() function (used to close an opened file).

  • Close the read file (input file) with the close() function (used to close an opened file)

Example

The following program copy only odd lines of a text file to another text file and prints the result odd lines −

# input text file inputFile = "ExampleTextFile.txt" # Opening the given file in read-only mode. readFile = open(inputFile, "r") # output text file path outputFile = "PrintOddLines.txt" # Opening the output file in write mode. writeFile = open(outputFile, "w") # Read the above read file lines using readlines() ReadFileLines = readFile.readlines() # Traverse in each line of the read text file for excelLineIndex in range(0, len(ReadFileLines)): # Checking whether the line number i.e excelLineIndex is even or odd # Here modulus 2 i.e %2 gives 1 for odd number and 0 for even number if(excelLineIndex % 2 != 0): # If the index is odd, then x`write the read file line into the # output file writeFile.write(ReadFileLines[excelLineIndex]) # printing the odd line print(ReadFileLines[excelLineIndex]) # Closing the write file writeFile.close() # Closing the read file readFile.close()

Output

On executing, the above program will generate the following output −

This is the Tutorials Point sample File
source codes in Python, Seaborn,Scala
Welcome everyone

We gave our program a text file containing some random content and then opened it in reading mode. The readlines() function was then used to retrieve a list of all the lines in the file, which we saved in a variable. We traversed the file until we reached the entire number of lines and checked whether the line number was odd or not. If it was the odd line, we appended it to a new file and printed it.

Conclusion

So far, we've learnt how to open a file, read its lines, and traverse its lines by index, which may be used to obtain information such as the values of the nth index line or the nth row in an excel sheet. Additionally, we discussed how to retrieve the value of a line by index and write that data to a file.

Updated on: 18-Aug-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements