Python文件操作类操作实例详解
更新时间:2014年07月11日 10:59:47 投稿:shichen2014
这篇文章主要介绍了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 import与from import使用和区别解读
Python程序可以调用一组基本的函数(即内建函数),比如print()、input()和len()等函数。接下来通过本文给大家介绍Python import与from import使用及区别介绍,感兴趣的朋友一起看看吧2021-09-09在Python中画图(基于Jupyter notebook的魔法函数)
这篇文章主要介绍了在Python中画图(基于Jupyter notebook的魔法函数),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2019-10-10python 使用 requests 模块发送http请求 的方法
本文分步骤给大家介绍了python 使用 requests 模块发送http请求 的方法,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下2018-12-12
最新评论