WRITE FILE IN PYTHON

GOCOURSE


Write

The write method writes a specified text to the file. If we open the file in 'W', it creates a new file on the name specified and returns the file object reference. If the specified file is already existing, the content will be erased.

Keyword:

 Write()

Syntax:

 file_object.write('string')

The write() method accepts a string as a parameter and writes the given string value into the specified file. After successful write operation is over, it returns the number of characters written into the file.

Program:


  # Open the file for writing
  file = open("example.txt", "w")

  # Write some text to the file
  file.write("Hello, world!\n")

  # Close the file
  file.close()




More topic in Python

Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send