Easy Tutorial
❮ Ref Math Tanh Python Os Dup2 ❯

Python3 File writelines() Method

Python3 File Methods


Overview

The writelines() method is used to write a sequence of strings to a file.

This sequence of strings can be generated by an iterable object, such as a list of strings.

Newlines need to be specified with the newline character \n.

Syntax

The syntax for the writelines() method is as follows:

fileObject.writelines( [ str ])

Parameters

-

str -- The sequence of strings to be written to the file.

Return Value

This method does not return any value.

Example

The following example demonstrates the use of the writelines() method:

#!/usr/bin/python3

# Open the file
fo = open("test.txt", "w")
print("File name: ", fo.name)
seq = ["tutorialpro.org 1\n", "tutorialpro.org 2"]
fo.writelines(seq)

# Close the file
fo.close()

The output of the above example is:

File name:  test.txt

View the file content:

$ cat test.txt 
tutorialpro.org 1
tutorialpro.org 2

Python3 File Methods

❮ Ref Math Tanh Python Os Dup2 ❯