`write()` Method
The write() method writes any string (binary data and text data) to an open file. The write() method does not add a newline character ('\n') to the end of the string.
Syntax:
fileobject.write(string)
Here, the passed parameter is the content to be written into the opened file.
Example:
# creates a new file sample.txt give write permissions on file
f = open('sample.txt', 'w')
# writing content into file sample.txt using write method
f.write("Python is a Programming language.")
f.close()
Output:
Python is a Programming language.
`writelines()` Method
The writelines() writes a sequence of strings to the file. The sequence can be any iterable object producing strings, typically a list of strings. There is no return value.
Syntax:
fileobject.writelines(sequence)
The sequence parameter is the sequence of the strings.
Example:
# creates a new file sample.txt give write permissions on file
f = open('sample.txt', 'w')
# writing content into file using write method
f.writelines(['python is easy\n', 'python is portable\n', 'python is comfortable'])
f.close()
Output:
python is easy
python is portable
python is comfortable