Python

Python Menu

Python

Python File writelines() - Definition, Syntax, Parameters, Examples

Definition

The writelines() method writes the items of a list to the file.

Where the texts will be inserted depends on the file mode and stream position.

  • "a" - The texts will be inserted at the current file stream position, default at the end of the file.
  • "w" - The file will be emptied before the texts will be inserted at the current file stream position, default 0.

Syntax

file.writelines(list)

Parameters

Parameter Description
list The list of texts or byte objects that will be inserted.

Examples:

f = open("myFile.txt", "a")
f.writelines(["write me!", "and me too."])
f.close()
f = open("myFile.txt", "a")
f.writelines(["\nWrite me!", "\nAnd me too!"])
f.close()

Introduction

Python Basics

Python Advance

Data Science Python Tutorials

Python Functions and Methods