Computer Science
Write a command(s) to write the following lines to the text file named hello.txt. Assume that the file is opened in append mode.
“ Welcome my class”
“It is a fun place”
“You will learn and play”
Python File Handling
1 Like
Answer
file = open("hello.txt", "a")
lines = [
"Welcome my class\n",
"It is a fun place\n",
"You will learn and play"
]
file.writelines(lines)
file.close()
Answered By
1 Like
Related Questions
Why is it advised to close a file after we are done with the read and write operations? What will happen if we do not close it ? Will some error message be flashed ?
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 Python program to open the file hello.txt used in question no 6 in read mode to display its contents. What will be the difference if the file was opened in write mode instead of append mode?
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.