Computer Science
Write a function to insert a sentence in a text file, assuming that text file is very big and can't fit in computer's memory.
Python Data Handling
3 Likes
Answer
Let the file "insert.txt" include the following sample text:
Bees hum
leaves rustle
Waves crash
nature's voice whispers
def insert_sentence(file_path, sentence):
file = open(file_path, 'a')
file.write(sentence + '\n')
file.close()
insert_sentence("insert.txt", "life's essence glimmers")
The "insert.txt" file includes following text:
Bees hum
leaves rustle
Waves crash
nature's voice whispers
life's essence glimmers
Answered By
3 Likes
Related Questions
What is the difference between the following set of statements (a) and (b):
(a)
P = open("practice.txt", "r") P.read(10)
(b)
with open("practice.txt", "r") as P: x = P.read()
Write a program to accept string/sentences from the user till the user enters “END” to. Save the data in a text file and then display only those sentences which begin with an uppercase alphabet.
Write a program to read a file 'Story.txt' and create another file, storing an index of 'Story.txt', telling which line of the file each word appears in. If word appears more than once, then index should-show all the line numbers containing the word.
Write a program to accept a filename from the user and display all the lines from the file which contain Python comment character '#'.