python文件读写
来源:互联网 发布:淘宝代理靠谱吗 编辑:程序博客网 时间:2024/06/10 14:56
python读文件和写文件都是采用open()函数,两者的区别是传入的标识符不一样,读文件传入’r(读文本文件)’或’rb’(读二进制文件),写文件传入‘w’(写文本文件)或’wb’(写二进制文件),读写完文件后都用调用close(),将文件对象关闭读写文件不一定会成功,可能会出现异常,和java一样,python读写文件需要捕获异常,可以使用try…finally 来捕获异常并关闭文件,不过python提供了更简洁的方法来自动调用close语句。
读文件并且自动调用close()
with open('path/file','r') as file: print(file.read())
写文件并自动调用close()
with open('path/file/','w') as file: file.write('hello python')
0 0
- python--python读写文件
- Python读写Unicode文件
- python Ini文件读写
- Python读写文件
- Python读写文件
- Python读写ini文件
- Python读写文件
- Python读写文件
- Python文件的读写
- python读写文件
- python文件读写
- [Python]读写文件方法
- python 文件读写操作
- Python文件读写操作
- Python读写文件
- python之读写文件
- python读写文件
- python文件的读写
- python的reduce函数和map函数
- Flask源码阅读(八)——请求钩子
- JqueryAjax登录实现
- Maven的初步使用以及命令行工具
- tyvj 1860后缀数组
- python文件读写
- 从测试和考试反射出的问题
- 命名空间和using使用
- java(11)--内省
- html jsp中<video>无法播放视频,该视频是由xuggler压缩图片流形成的
- UITableview性能优化总结
- BZOJ 3365 Distance Statistics 点分治
- SpringMVC+myBatis事务不生效
- smali文件对比java文件