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
output.txt).Step-by-Step Logic
Open input file in read mode
Read all lines using
readlines()Remove newline characters using
strip()Store cleaned lines in a list
Sort the list using
sort()Open output file in write mode
Write sorted lines into output file
Close files
π Key idea: Read → Clean → Store → Sort → Write
Flowchart (Block Diagram of Logic)
Start
Open input file
Read lines
Remove extra spaces/newlines
Store in list
Sort list
Open output file
Write sorted content
Stop
Algorithm
Start
Open input file in read mode
Read lines using
readlines()Create empty list
For each line:
Remove newline using
strip()Append to list
Sort the list
Open output file in write mode
Write each sorted element into file
Close files
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)
Output File (output.txt)
Console Output:
Important Concepts Used
File handling
open()readlines()write()strip()append()sort()List data structure
Viva Voce Questions
What does
readlines()return?Why do we use
strip()?What is the default sorting order?
How to sort in reverse order?
What happens if file does not exist?
What is the difference between read() and readlines()?
Why do we add
"\n"while writing?
Procedure to Execute the Program
Create a text file (example:
input.txt)Add some lines of text
Save the file
Run the Python program
Enter input and output file names
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-exceptIgnore 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.

Comments
Post a Comment