Adding Content to a Text File - Python Programming Exercise

In this exercise, you will develop a Python program that prompts the user to input multiple sentences, stopping when they press Enter without typing anything. This exercise is perfect for practicing file handling, loops, and user input in Python. By implementing this program, you will gain hands-on experience in handling file operations, loops, and user input in Python. This exercise not only reinforces your understanding of file handling but also helps you develop efficient coding practices for managing user interactions.

 Category

Managing Files

 Exercise

Adding Content To A Text File

 Objective

Develop a Python program that prompts the user to input multiple sentences, stopping when they press Enter without typing anything. The program should save these sentences in a text file called "sentences.txt." If the file already exists, the program must append the new input to the end of the existing content.

 Example Python Exercise

 Copy Python Code
def collect_and_save_sentences():
    """
    Collects multiple sentences from the user and saves them to a file.
    If the file already exists, appends the new sentences to the end.
    Stops when the user presses Enter without typing anything.
    """
    sentences = []
    
    print("Enter sentences. Press Enter without typing anything to finish.")
    
    while True:
        sentence = input("Enter a sentence: ")
        
        # If the user presses Enter without typing anything, stop collecting sentences
        if not sentence:
            break
        
        # Add the sentence to the list
        sentences.append(sentence)
    
    # Append the sentences to 'sentences.txt', creating the file if it doesn't exist
    with open("sentences.txt", "a") as file:
        for sentence in sentences:
            file.write(sentence + "\n")
    
    print(f"\n{len(sentences)} sentences were added to 'sentences.txt'.")


if __name__ == "__main__":
    collect_and_save_sentences()

 Output

Enter sentences. Press Enter without typing anything to finish.
Enter a sentence: First sentence.
Enter a sentence: Another sentence.
Enter a sentence: 
2 sentences were added to 'sentences.txt'.

 Share this Python Exercise

 More Python Programming Exercises of Managing Files

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Show File Data

    In this exercise, you will develop a Python program to read and display the contents of a text file. This exercise is perfect for practicing file handling, com...

  •  TextToHTML with File Integration

    In this exercise, you will develop a Python program to enhance the TextToHTML class by adding the ability to save its results into a text file. This exercise i...

  •  Log Handler

    In this exercise, you will develop a Python program with a class called Logger, which includes a static method called log. This exercise is perfect for practic...

  •  More

    In this exercise, you will develop a Python program that mimics the behavior of the Unix "more" command. This exercise is perfect for practicing file handling,...

  •  Text Modifier

    In this exercise, you will develop a Python program that reads a text file, replaces specified words, and saves the modified content into a new file. This exercise...

  •  Count characters in a text file

    In this exercise, you will develop a Python program that counts how many times a specific character appears in a given file (of any type). This exercise is per...