file.writelines(sequence)

本页内容
上一节: Python3_file_write

Python3 File writelines() 方法

Python3 File(文件) 方法

概述

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

Python3 File(文件) 方法

上一节: Python3_file_write
此页面最后编辑于2022年8月17日 (星期三) 21:08。