Python File writelines() 方法

Python 教程 · 2019-02-03 16:42:10

概述

writelines() 方法用于向文件中写入一序列的字符串。

这一序列字符串可以是由迭代对象产生的,如一个字符串列表。

换行需要制定换行符 \n。

语法

writelines() 方法语法如下:

fileObject.writelines( [ str ])

参数

  • str -- 要写入文件的字符串序列。

返回值

该方法没有返回值。

实例

以下实例演示了 writelines() 方法的使用:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 打开文件
fo = open("test.txt", "w")
print "文件名为: ", fo.name
seq = ["码农教程 1\n", "码农教程 2"]
fo.writelines( seq )

# 关闭文件
fo.close()

以上实例输出结果为:

文件名为:  test.txt

查看文件内容:

$ cat test.txt 
码农教程 1
码农教程 2

点击查看所有 Python 教程 文章: https://codercto.com/courses/l/8.html

查看所有标签

The Definitive Guide to MongoDB

The Definitive Guide to MongoDB

Peter Membrey、Wouter Thielen / Apress / 2010-08-26 / USD 44.99

MongoDB, a cross-platform NoSQL database, is the fastest-growing new database in the world. MongoDB provides a rich document orientated structure with dynamic queries that you’ll recognize from RDMBS ......一起来看看 《The Definitive Guide to MongoDB》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

html转js在线工具
html转js在线工具

html转js在线工具