Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Merge the last 3 lines of each text file with specific arrangement

I have high number of txt files in E:\Desktop\Social_media\edit8\New folder directory and each file has an arrangement similar to the following:

Bolt
2,739,393
Classmates
1,267,092
SixDegrees
1,077,353
PlanetAll
552,488
theGlobe
437,847
OpenDiary
9,251
1998
MARCH
034+

Now I want to merge each txt file last 3 lines like following:

Bolt
2,739,393
Classmates
1,267,092
SixDegrees
1,077,353
PlanetAll
552,488
theGlobe
437,847
OpenDiary
9,251
034+ MARCH 1998

this mean last 3 lines must have an arrangement like number+ month year

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I write following python script for this but I don’t know why not working:

import os

# Define the directory where your text files are located
directory_path = r'E:\Desktop\Social_media\edit8\New folder'

# Function to rearrange the lines and write to a new file
def rearrange_lines(file_path):
    with open(file_path, 'r') as file:
        lines = [line.strip() for line in file.readlines() if line.strip()]  # Read non-empty lines

    # Check if there are at least 3 non-empty lines
    if len(lines) >= 3:
        lines[-1], lines[-2], lines[-3] = lines[-3], lines[-2], lines[-1]  # Rearrange the last 3 lines

        # Create a new file with the rearranged lines
        with open(file_path, 'w') as file:
            file.write('\n'.join(lines))

# Iterate through each file in the directory
for root, dirs, files in os.walk(directory_path):
    for file_name in files:
        if file_name.endswith('.txt'):
            file_path = os.path.join(root, file_name)
            rearrange_lines(file_path)
            print(f'Rearranged lines in {file_name}')

print('Done!')

Where is my script problem? and how to fix it problem?

>Solution :

You’re not combining the last 3 lines into a single line in the result.

# Function to rearrange the lines and write to a new file
def rearrange_lines(file_path):
    with open(file_path, 'r') as file:
        lines = [line.strip() for line in file.readlines() if line.strip()]  # Read non-empty lines

    # Check if there are at least 3 non-empty lines
    if len(lines) >= 3:
        # reverse last 3 lines, join them with space, and replace with this result
        lines[-3:] = [' '.join(lines[-1:-4:-1])]

        # Create a new file with the rearranged lines
        with open(file_path, 'w') as file:
            file.write('\n'.join(lines))

Assigning to lines[-3:] is a slice replacement. We replace this with a list of a single line.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading