Python文件操作类操作实例详解
2019/7/13 21:49:22
本文主要是介绍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文件操作类操作实例详解的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-04Python外包有哪些常见的主要库-icode9专业技术文章分享
- 2024-10-02基于深度学习的钢铁缺陷检测系统(yolov8、注意力机制、PyQt5界面、Python)
- 2024-10-01怎么用python搭建一个系统-icode9专业技术文章分享
- 2024-09-30Python基础入门教程
- 2024-09-30获取参数学习:Python中的基础教程
- 2024-09-30Python编程基础详解
- 2024-09-29点击加载学习:Python编程基础教程
- 2024-09-29数据科学五大Python前端库:第二部分
- 2024-09-27使用python 将ETH账户的资产打散
- 2024-09-26Python编程基础