Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 File writelines() Method

Python3 File writelines() Method

Python3 File Methods Python3 File methods


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

writelines() writes all strings in the iterable to the file in order. The write position depends on 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:

   

print

(

"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

  1. Does not add newline characters automatically:

  2. 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。

  3. -

    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。

    1. File mode:

    2. If the file is opened in read-only mode ("r"), calling writelines() will throw io.UnsupportedOperation exception.

    3. If the file is opened in write mode ("w" or "w+"), the file contents are cleared and new contents are written.

    4. If the file is opened in append mode ("a" or "a+"), the written content is added to the end of the file.

    5. -

      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.

      1. File pointer:

      2. 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.

      3. -

        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.

        1. Iterable objects:

        2. sequence It can be any iterable object (such as a list, tuple, or generator), but each element must be a string.

        3. -

          sequence It can be any iterable object (such as a list, tuple, or generator), but each element must be a string.


          Python3 File Methods Python3 File methods