Program6

 6. Develop a program to sort the contents of a text file and write the sorted contents into a separate text file. [Hint: Use string methods strip(), len(), list methods sort(), append(), and file methods open(), readlines(), and write()].


Objective of the Program

  • To understand file handling in Python

  • To read file line-by-line

  • To store file contents in a list

  • To sort list elements

  • To write output into a new file


Logical Construction (Think Before You Code)

Suppose input file input.txt contains: 

Banana

Apple

Mango

Orange

After sorting (alphabetically):

Apple

Banana

Mango

Orange

This sorted content must be written into another file (example: output.txt).


Step-by-Step Logic

  1. Open input file in read mode

  2. Read all lines using readlines()

  3. Remove newline characters using strip()

  4. Store cleaned lines in a list

  5. Sort the list using sort()

  6. Open output file in write mode

  7. Write sorted lines into output file

  8. Close files

πŸ‘‰ Key idea: Read → Clean → Store → Sort → Write


Flowchart (Block Diagram of Logic)


Flowchart Explanation
  • Start

  • Open input file

  • Read lines

  • Remove extra spaces/newlines

  • Store in list

  • Sort list

  • Open output file

  • Write sorted content

  • Stop


Algorithm

  1. Start

  2. Open input file in read mode

  3. Read lines using readlines()

  4. Create empty list

  5. For each line:

    • Remove newline using strip()

    • Append to list

  6. Sort the list

  7. Open output file in write mode

  8. Write each sorted element into file

  9. Close files

  10. Stop


Python Program

⚠ Students must complete the missing parts.

# Program to sort contents of a text file


input_file = input("Enter input file name: ")

output_file = input("Enter output file name: ")


file1 = open(input_file, "r")

lines = file1.____________()

file1.close()


clean_lines = []


for line in lines:

    cleaned = line.____________()

    clean_lines.____________(cleaned)


clean_lines.____________()


file2 = open(output_file, "w")


for item in clean_lines:

    file2.____________(item + "\n")


file2.close()


print("File sorted successfully.")


Probable Output

Input File (input.txt)

Banana
Apple
Mango
Orange

Output File (output.txt)

Apple
Banana
Mango
Orange

Console Output:

Enter input file name: input.txt
Enter output file name: output.txt
File sorted successfully.

Important Concepts Used

  • File handling

  • open()

  • readlines()

  • write()

  • strip()

  • append()

  • sort()

  • List data structure


Viva Voce Questions

  1. What does readlines() return?

  2. Why do we use strip()?

  3. What is the default sorting order?

  4. How to sort in reverse order?

  5. What happens if file does not exist?

  6. What is the difference between read() and readlines()?

  7. Why do we add "\n" while writing?


Procedure to Execute the Program

  1. Create a text file (example: input.txt)

  2. Add some lines of text

  3. Save the file

  4. Run the Python program

  5. Enter input and output file names

  6. Open output file to verify sorted result


Google Colab – Online Execution

πŸ‘‰ Click the link below to run the complete working version of this program online:
[https://colab.research.google.com/drive/1v0FV9CXYcgbERXZoBKfbKTQoqD3P2q0R?usp=sharing]


Program Explanation



Assignment for Students

  • Sort in reverse order

  • Sort based on length of each line

  • Handle file-not-found using try-except

  • Ignore empty lines



Lab Record Instructions

Right Side of the Record
1. Problem Statement
2. Python Program
Write the following neatly on the right-hand side page:


Left Side of the Record
Write the following neatly on the left-hand side page:
3. Flowchart
4. Algorithm
5. Output
⚠️ Students must write ALL possible outputs.



-:END:-

Comments

Popular posts from this blog