Write a statement to write the string “Welcome! Please have a seat” in a file wel.txt, that has not yet been opened.
Answer:
f1 = open ("wel.txt", 'w')
f1.write ("Welcome! Please have a seat")
Here,
- f1 is the file object.
- open( ) method opens the file for writing to it.
- “wel.text” is the file name.
- ‘w’ is the access mode ie., the file is opened to write to it.
- write( ) method writes the specified string to the file.