This Python program receives multiple sentences from the user and appends them to the end of a text file named 1188.txt located in the Data folder on drive D:.
This method is useful for updating logs or notes without overwriting existing content.
# Ask user how many sentences to add
n = int(input("How many sentences do you want to add? "))
# Collect sentences in a list
sentences = []
for i in range(n):
sentence = input(f"Sentence {i+1}: ")
sentences.append(sentence)
# File path in Data folder on drive D
file_path = "D:/Data/1188.txt"
# Append sentences to the file
with open(file_path, "a", encoding="utf-8") as f:
for line in sentences:
f.write(line + "\n")
print("Sentences successfully appended to the file.")
How many sentences do you want to add? 2
Sentence 1: This is a new sentence
Sentence 2: Second sentence for testing
Sentences successfully appended to the file.
- The user specifies how many sentences to add
- Each sentence is collected using input() and stored in a list
- The file 1188.txt is opened in "a" (append) mode to preserve existing content
- Each sentence is written to the end of the file
- A success message confirms the operation