Python Program to Copy Odd Lines of Text File to Another File

Files in Python:

Files are identified locations on the disc where associated data is stored. They’re used to keep data in non-volatile memory for a long time (e.g. hard disk).

We use files for future usage of the data by permanently saving it because Random Access Memory (RAM) is volatile (it loses its contents when the machine is turned off).

We must first open a file before we can read from or write to it. When we’re finished, it needs to be closed so that the file’s resources may be released.

As a result, a file operation in Python is performed in the following order:

  • Create a new file
  • You can either read or write (perform the operations)
  • Close the file.

Given a file, the task is to copy odd lines from a given file into another file in Python.

Program to Copy Odd Lines of Text File to Another File in Python

Below is the full approach for copying odd lines from a given file into another file in Python

Approach:

  • Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
  • Open the file in read-only mode. In this case, we’re simply reading the contents of the file.
  • Make another variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
  • Open another file in write mode. In this case, we’re writing the contents into the file.
  • Get the lines of the reading file using the readlines() function and store it in a variable.
  • Iterate in the length of the file lines using the for loop.
  • Check if the index of the file lines is odd using the if conditional statement.
  • If it is true, then write the file line using the write() function into the writing file.
  • Close the writing file using the close() function.
  • Close the reading file using the close() function.
  • The Exit of Program.

Below is the implementation:

# Make a single variable to store the path of the file. This is a constant value.
# This value must be replaced with the file path from your own system in the example below.
givenFilename = "samplefile.txt"
# Open the file in read-only mode. In this case, we're simply reading the contents of the file.
reading_file = open(givenFilename, "r") 

# Make another variable to store the path of the file. This is a constant value.
# This value must be replaced with the file path from your own system in the example below.
givenFilename = "oddlinesfile.txt"
# Open another file in write mode. In this case, we're writing the contents into the file.
writing_file = open(givenFilename, "w") 

# Get the lines of the reading file using the readlines() function and store it in a variable
filelines = reading_file.readlines() 
# Iterate in the length of the file lines using the for loop
for line_indx in range(0, len(filelines)): 
    # Check if the index of the file lines is odd using the if conditional statement
    if(line_indx % 2 != 0): 
        # If it is true, then write the file line using the write() function into the 
        # writing file
        writing_file.write(filelines[line_indx]) 

# Close the writing file using the close() function
writing_file.close()
# Close the reading file using the close() function
reading_file.close() 

Output:

this 
btechgeeks
morning
welcome

File Content: (samplefile.txt)

hello 
this 
is 
btechgeeks
good 
morning
btechgeeks
welcome 
all

File Content: (oddlinesfile.txt)

this 
btechgeeks
morning
welcome

Google Colab Images:

Files and Code: