Home  >  Article  >  Backend Development  >  What is Python's File write lines() method? What are the functions?

What is Python's File write lines() method? What are the functions?

乌拉乌拉~
乌拉乌拉~Original
2018-08-16 17:36:532881browse

In today’s article, we will learn about Python’s Filewritelines() method. In this article, I will explain the write method and its related lines method in Python and We will also explain where this write() method can be used in python programming. Without further ado, let’s get started.

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.

Newline requires specifying the newline character \n.

Syntax

The writelines() method syntax is as follows:

fileObject.writelines( [ str ])

Parameters

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

(This method has no return value.)

Example

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

#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 打开文件
fo = open("test.txt", "w")
print "文件名为: ", fo.name
seq = ["蓝色天空 1\n", "蓝色天空 2"]
fo.writelines( seq )

The output result of the above example is:

文件名为:  test.txt

View the file content:

$ cat test.txt 
蓝色天空1
蓝色天空2

In this article, we explain what the writelines() method is. If you don’t understand, you can Give it a try. After all, hands-on practice is the best way to verify what you have learned. Finally, I hope this article can bring some help to you who are learning python.

For more related knowledge, please visit the Python tutorial column on the php Chinese website.

The above is the detailed content of What is Python's File write lines() method? What are the functions?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn