Python File writelines() 方法

栏目: 编程语言 · Python · 编程教程 · python 教程 · 发布时间: 7年前

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

概述

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

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

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

语法

writelines() 方法语法如下:

fileObject.writelines( [ str ])

参数

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

返回值

该方法没有返回值。

实例

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

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

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

# 关闭文件
fo.close()

以上实例输出结果为:

文件名为:  test.txt

查看文件内容:

$ cat test.txt 
www.codercto.com 1
www.codercto.com 2

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Letting Go of the Words

Letting Go of the Words

Janice (Ginny) Redish / Morgan Kaufmann / 2007-06-11 / USD 49.95

"Redish has done her homework and created a thorough overview of the issues in writing for the Web. Ironically, I must recommend that you read her every word so that you can find out why your customer......一起来看看 《Letting Go of the Words》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

在线进制转换器
在线进制转换器

各进制数互转换器

SHA 加密
SHA 加密

SHA 加密工具