Python3 File writelines() Method
writelines() is a method of Python file objects that writes a list of strings (or any iterable) to a file.
The difference between write() Unlike the method, writelines() can write multiple lines at once but does not add newline characters automatically.
Newlines require specifying the newline character \n.
Syntax
The writelines() method syntax is as follows:
fileObject.writelines( [ sequence ])
Parameters
-
fileObject: a file object, usually obtained byopen()opening a file with the function. -
sequence: an iterable (such as a list or tuple), each element of which must be a string. -
Return Value: no return value (returns
None)。
writelines() writes all strings in the iterable to the file in order. The write position depends on the current file pointer position:
-
If the file is opened in append mode (
"a"or"a+"), the written content is added to the end of the file. -
If the file is opened in read/write mode (
"r+"or"w+"), the written content overwrites the existing content starting at the current file pointer position.
Return Value
This method has no return value.
Example
The following example shows how to use the writelines() method:
Example
#!/usr/bin/python3
Open the file with a with statement to ensure it is closed properly
with
open
(
"test.txt"
,
"w"
)
as
fo:
(
"File name: "
,
fo.
name
)
seq
=
[
"Runoob Tutorial 1\n"
,
"Runoob Tutorial 2"
]
fo.
writelines
(
seq
)
The output of the above example is:
文件名为: test.txt
View the file contents:
$ cat test.txt
Runoob Tutorial 1
Runoob Tutorial 2
Notes
-
Does not add newline characters automatically:
writelines() It does not add a newline at the end of each line automatically. If newlines are needed, they must be added explicitly in each string \n。
writelines() It does not add a newline at the end of each line automatically. If newlines are needed, they must be added explicitly in each string \n。
-
File mode:
If the file is opened in read-only mode ("r"), calling writelines() will throw io.UnsupportedOperation exception.
If the file is opened in write mode ("w" or "w+"), the file contents are cleared and new contents are written.
If the file is opened in append mode ("a" or "a+"), the written content is added to the end of the file.
If the file is opened in read-only mode ("r"), calling writelines() will throw io.UnsupportedOperation exception.
If the file is opened in write mode ("w" or "w+"), the file contents are cleared and new contents are written.
If the file is opened in append mode ("a" or "a+"), the written content is added to the end of the file.
-
File pointer:
Writing starts at the current file pointer position. To write from the beginning of the file or a specific position, use the seek() method to move the file pointer.
Writing starts at the current file pointer position. To write from the beginning of the file or a specific position, use the seek() method to move the file pointer.
-
Iterable objects:
sequence It can be any iterable object (such as a list, tuple, or generator), but each element must be a string.
sequence It can be any iterable object (such as a list, tuple, or generator), but each element must be a string.
Python3 File methods