Check sibling questions

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.
  1. Computer Science - Class 12
  2. Chapter 2 - File Handling in Python

About the Author

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science and Computer Science at Teachoo