关于python的文件操作
来源:互联网 发布:网络电视能看百度云吗 编辑:程序博客网 时间:2024/05/16 12:56
__author__ = 'Kaiming' #感谢廖大评论区的网友@Kaiming#P1 打开文件、读文件、关闭文件的典型方法try: f=open('D:/test.txt','r') print(f.read())finally: if f: f.close()#P2 推荐的简洁写法,不必显示的关闭文件描述符#open返回的对象在python中称作file-like 对象,可以是字节流、网络流、自定义流等with open('D:/test.txt','r') as f: #按行读取 for line in f.readlines(): print(line.strip())#P3 直接读取二级制的图片、视频文件# with open('D:/banner.jpg','rb') as f2:# for line in f2.readlines():# print(line.strip())#P4 可以指定编码读取相应的数据,还可以忽略非法编码with open('D:/test.txt','r',encoding='gbk',errors='ignore') as f3: for line in f3.readlines(): print(line.strip())#P5 写文件的流程和读文件是一样的 代开文件、写入内容、关闭文件# 'r' open for reading (default)# 'w' open for writing, truncating the file first# 'x' open for exclusive creation, failing if the file already exists# 'a' open for writing, appending to the end of the file if it exists# 'b' binary mode# 't' text mode (default)# '+' open a disk file for updating (reading and writing)# 'U' universal newlines mode (deprecated)with open('D:/test12.txt','a+') as f4: for line in f4.readlines(): print(line.strip()) f4.write('a new line2!')
0 0
- python关于文件的操作
- 关于Python的文件操作
- 关于python的文件操作
- Python关于文件的操作
- 关于python文件操作
- 关于python文件操作
- 关于python文件操作
- 关于python文件操作
- 关于python文件操作
- 关于python文件操作
- 关于python文件操作
- 关于python文件操作
- 关于python文件操作
- 关于python文件操作
- 关于python文件操作
- 关于python文件操作
- 关于python文件操作
- 关于python文件操作
- Apache Stratos探究:配置CLI
- The working copy "" failed to commit files
- 自动释放池
- html5之meta charset网页字符编码简写
- [leetcode] 156. Binary Tree Upside Down 解题报告
- 关于python的文件操作
- java中static作用详解
- 人工智能实战——人工神经网络(C库iOS交叉编译)
- Make sure you have Java JDK or JRE installed and the required ports are free
- 更改mac下svg的打开方式
- [数据存储之六]plist文件读取
- 关于沉浸模式 状态栏颜色 底部虚拟键相关
- mybatis typeHandler自定义类型转换器
- AS混淆基本配置解释及语法