Home > Article > Backend Development > How to copy odd lines of a text file into another file using Python
In this article, we will show you how to copy odd-numbered lines of a text file to another text file using Python.
Suppose we obtain a text file named TextFile.txt that contains some random text. We just copy all the odd lines of one text file into another text file 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
The following are the algorithms/steps to perform the required task:
Create a variable to store the path to the text file.
Open a text file in read-only mode using the open() function (which opens a file and returns a file object as a result) by passing the filename and mode as its arguments (here "r " represents read-only mode).
readFile = open(inputFile, "r")
Create a variable to store the output file path containing only odd lines from the given input file.
Open the output file in write mode using the open() function (which opens a file and returns a file object as a result) by passing the file name and mode as parameters (here "w" represents writing mode).
Using the readlines() function (returns a list where each line in the file is represented as a list item. To limit the number of lines returned, use the hint argument. No longer if returned The total number of bytes exceeds the specified number, returns lines to get a list of lines for the given input text file.
file.readlines(hint)
Use a for loop to iterate through each line of the read text file until the length of the file. Use the len() function (the len() method returns the number of items in the object) to calculate the length of the read file.
Use the if conditional statement to determine whether the read file line index is an odd number.
If the condition is true, the file lines read will be read using the write() function (writes the specified text to the file. The provided text will be inserted based on the file mode and stream position) Write to output file.
Print odd lines in the given input file.
Use the close() function to close the write file (output file) (used to close the open file).
Use the close() function to close the read file (input file) (used to close the open file)
The following program copies only the odd lines of a text file to another text file and prints the odd lines of the result -
# 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()
When executed, the above program will generate the following output -
This is the Tutorials Point sample File source codes in Python, Seaborn,Scala Welcome everyone
We provide the program with a text file containing some random content and then open it in read mode. Then use the readlines() function to get a list of all lines in the file and save it in a variable. We iterate through the file until we reach the number of all lines and check if the line number is odd or even. If it's an odd number of lines, we append it to a new file and print it.
So far, we have learned how to open a file, read file rows, and traverse file rows by index, which can be used to obtain information such as the nth index row or the value of the nth row in an excel sheet. Additionally, we discussed how to retrieve a row's value through an index and write that data to a file.
The above is the detailed content of How to copy odd lines of a text file into another file using Python. For more information, please follow other related articles on the PHP Chinese website!