Class 12 Programs
- 1.Read text file line by line with # separator
- 2.File handling - Read and Write
- 3.Count words in a file
- 4.Search text in a file
- 5.Copy content from one file to another
- 6.CSV file operations
- 7.Binary file handling
- 8.Stack implementation
- 9.Queue implementation
- 10.Database connectivity with MySQL
Quick Tips
- • Always use 'with' statement for file handling
- • Remember to handle file exceptions
- • Use appropriate file modes (r, w, a)
Read a text file line by line and display each word separated by a #
AIM
To read a text file line by line and display each word separated by a # symbol.
ALGORITHM
- Open the text file in read mode.
- Read the file line by line using a loop.
- For each line, remove leading/trailing whitespaces using strip().
- Split the line into words using split().
- Join the words using # as the separator using join().
- Print the joined words.
- Close the file.
PROGRAM
# Open the file in read mode
file = open("sample.txt", "r")
# Read the file line by line
for line in file:
line = line.strip() # Remove leading/trailing whitespace or newline
words = line.split() # Split line into words
output = '#'.join(words) # Join words using #
print(output) # Display the formatted line
# Close the file
file.close()Content of sample.txt file:
CBSE Computer Science Class 12
File handling is an important topic
Python makes file processing easy
OUTPUT
CBSE#Computer#Science#Class#12
File#handling#is#an#important#topic
Python#makes#file#processing#easy
RESULT
The program executed successfully and displayed each word from the text file separated by a # symbol, as required.
KEY POINTS
- strip(): Removes leading and trailing whitespaces including newline characters
- split(): Splits a string into a list of words based on whitespace
- join(): Joins elements of a list into a string using a specified separator
- File handling: Always close the file after reading to free up system resources
PROGRAM VARIATIONS
Method 1: Using 'with' statement (Recommended)
# Using 'with' statement for better file handling
with open("sample.txt", "r") as file:
for line in file:
line = line.strip()
words = line.split()
output = '#'.join(words)
print(output)
# File is automatically closedMethod 2: Reading all lines at once
# Reading all lines at once
with open("sample.txt", "r") as file:
lines = file.readlines()
for line in lines:
line = line.strip()
words = line.split()
output = '#'.join(words)
print(output)COMMON ERRORS & SOLUTIONS
FileNotFoundError
Make sure the file "sample.txt" exists in the same directory as your Python script.
Not closing the file
Always close the file using file.close() or use 'with' statement for automatic closure.
Empty lines in output
Use strip() to remove whitespaces and check if line is not empty before processing.
VIVA QUESTIONS
What is the purpose of strip() function?
The strip() function removes leading and trailing whitespaces, including newline characters from a string.
What does split() function do?
The split() function splits a string into a list of words based on whitespace characters (spaces, tabs, newlines).
How does join() function work?
The join() function joins elements of a list into a single string using a specified separator. Here, '#'.join(words) joins words with # symbol.
Why should we close the file?
Closing the file frees up system resources and ensures that all data is properly written to the file.
What is the advantage of using 'with' statement?
The 'with' statement automatically closes the file even if an error occurs, making it a safer way to handle files.