Python文件操作类操作实例详解

yipeiwu_com5年前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)

相关文章

python 转换 Javascript %u 字符串为python unicode的代码

web采集的数据为 %u6B63%u5F0F%u4EBA%u5458,需要读取并转换为python对象,想了下不调用Javascript去eval,只能自己翻译了。 核心代码: i...

python2 与 pyhton3的输入语句写法小结

什么是输入 咱们在银行ATM机器前取钱时,肯定需要输入密码,对不? 那么怎样才能让程序知道咱们刚刚输入的是什么呢?? 大家应该知道了,如果要完成ATM机取钱这件事情,需要先从键盘中输入一...

python实现输出一个序列的所有子序列示例

python实现输出一个序列的所有子序列示例

如下所示: def sub(arr): finish=[] size = len(arr) end = 1 << size #end=2**size for in...

浅谈python3.6的tkinter运行问题

python3.6在运行tkinter时要选择 run as Python unit-test, 否则报错 ModuleNotFoundError: No module named 't...

python中sleep函数用法实例分析

本文实例讲述了python中sleep函数用法。分享给大家供大家参考。具体如下: Python中的sleep用来暂停线程执行,单位为秒 #----------------------...