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

查看所有标签

Landing Page Optimization

Landing Page Optimization

Tim Ash / Wiley Publishing / 2008-1-29 / USD 29.99

在线阅读本书 How much money are you losing because of poor landing page design? In this comprehensive, step-by-step guide, you’ll learn all the skills necessary to dramatically improve your bottom li......一起来看看 《Landing Page Optimization》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具