python操作实例_Python文件操作类操作实例详解

#!/usr/bin/env python

#!/usr/bin/env python

#coding:utf-8

# Purpose: 文件操作类

#声明一个字符串文本

poem='''

Programming is fun测试

When the work is done

if you wanna make your work also fun:

use Python!

'''

#创建一个file类的实例,模式可以为:只读模式('r')、写模式('w')、追加模式('a')

f=file('poem.txt','a') #open for 'w'riting

f.write(poem) #写入文本到文件 write text to file

f.close() #关闭文件 close the file

#默认是只读模式

f=file('poem.txt')

# if no mode is specified,'r'ead mode is assumed by default

while True:

line=f.readline() #读取文件的每一个行

if len(line)==0: # Zero length indicates EOF

break

print line, #输出该行

#注意,因为从文件读到的内容已经以换行符结尾,所以我们在输出的语句上使用逗号来消除自动换行。

#Notice comma to avoid automatic newline added by Python

f.close() #close the file

#帮助

help(file)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值