Python3 File writelines() 方法

Python 3 教程 · 2019-02-07 06:56:57

概述

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

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

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

语法

writelines() 方法语法如下:

fileObject.writelines( [ str ])

参数

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

返回值

该方法没有返回值。

实例

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

#!/usr/bin/python3

# 打开文件
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 3 教程 文章: https://codercto.com/courses/l/10.html

查看所有标签

Beginning Apache Struts

Beginning Apache Struts

Arnold Doray / Apress / 2006-02-20 / USD 44.99

Beginning Apache Struts will provide you a working knowledge of Apache Struts 1.2. This book is ideal for you Java programmers who have some JSP familiarity, but little or no prior experience with Ser......一起来看看 《Beginning Apache Struts》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具