发布于 2014-11-03 21:55:26 | 182 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Python教程,程序狗速度看过来!

Python编程语言

Python 是一种面向对象、解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年。Python语法简洁而清晰,具有丰富和强大的类库。它常被昵称为胶水语言,它能够把用其他语言制作的各种模块(尤其是C/C++)很轻松地联结在一起。


本文是一个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)


最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务