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

  1. Open the text file in read mode.
  2. Read the file line by line using a loop.
  3. For each line, remove leading/trailing whitespaces using strip().
  4. Split the line into words using split().
  5. Join the words using # as the separator using join().
  6. Print the joined words.
  7. 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 closed

Method 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

  1. What is the purpose of strip() function?

    The strip() function removes leading and trailing whitespaces, including newline characters from a string.

  2. What does split() function do?

    The split() function splits a string into a list of words based on whitespace characters (spaces, tabs, newlines).

  3. 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.

  4. Why should we close the file?

    Closing the file frees up system resources and ensures that all data is properly written to the file.

  5. 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.