Python学习VI --- 文件与目录
来源:互联网 发布:单片机rl 编辑:程序博客网 时间:2024/06/07 06:34
这篇博客主要是阅读python之旅 时做的笔记。提取出最主要的知识点,供个人在以后中快速查阅。
读写文本文件
读文件
try: f = open('/path/to/file', 'r') # 打开文件 data = f.read() # 读取文件内容finally: if f: f.close() # 确保文件被关闭#为啥不用with呢,它会自动调用close方法with open('/path/to/file', 'r') as f: data = f.read()
读取所有内容
重点说明一下readlines() ,其实就是把所有内容按行读取,放入一个列表中罢了。
10 1 9 96 3 2 820 10 3 231 4 1 1010 8 6 310 2 1 6with open('data.txt', 'r') as f: lines = f.readlines() line_num = len(lines) print lines print line_num['10\t1\t9\t9\n', '6\t3\t2\t8\n', '20\t10\t3\t23\n', '1\t4\t1\t10\n', '10\t8\t6\t3\n', '10\t2\t1\t6']6
按字节读取
当文件过大时,还是最好构造一个固定长度的缓冲区,不断读取文件内容
#简单版本with open('path/to/file', 'r') as f: while True: piece = f.read(1024) # 每次读取 1024 个字节(即 1 KB)的内容 if not piece: break print piece#用生成器def read_in_chunks(file_object, chunk_size=1024): """Lazy function (generator) to read a file piece by piece. Default chunk size: 1k.""" while True: data = file_object.read(chunk_size) if not data: break yield datawith open('path/to/file', 'r') as f: for piece in read_in_chunks(f): print piece
逐行读取
with open('data.txt', 'r') as f: while True: line = f.readline() # 逐行读取 if not line: break print line, # 这里加了 ',' 是为了避免 print 自动换行
文件迭代器
在 Python 中,文件对象是可迭代的,这意味着我们可以直接在 for 循环中使用它们,而且是逐行迭代的,也就是说,效果和 readline() 是一样的,而且更简洁。
# f就是文件迭代器,而且是逐行迭代的with open('data.txt', 'r') as f: for line in f: print line,
写文件
#覆盖模式with open('/Users/ethan/data2.txt', 'w') as f: f.write('one\n') f.write('two')#追加模式with open('/Users/ethan/data2.txt', 'a') as f: f.write('three\n') f.write('four')
读写二进制文件
对于二进制文件,我们要多加一个’b’
with open('test.png', 'rb') as f: image_data = f.read() # image_data 是字节字符串格式的,而不是文本字符串
这里需要注意的是,在读取二进制数据时,返回的数据是字节字符串格式的,而不是文本字符串。一般情况下,我们可能会对它进行编码,比如 base64 编码,可以这样做:
import base64with open('test.png', 'rb') as f: image_data = f.read() base64_data = base64.b64encode(image_data) # 使用 base64 编码 print base64_data
写入二进制文件
with open('test.png', 'rb') as f: image_data = f.read()with open('/Users/ethan/test2.png', 'wb') as f: f.write(image_data)
OS模块
OS主要是常见的文件和目录操作, 详情见官方文档
# 绝对路径>>> import os # 记得导入 os 模块>>> os.path.abspath('hello.py')'/Users/ethan/coding/python/hello.py'>>> os.path.abspath('web')'/Users/ethan/coding/python/web'>>> os.path.abspath('.') # 当前目录的绝对路径'/Users/ethan/coding/python'#获取文件或文件夹路径>>> os.path.dirname('/Users/ethan/coding/python/hello.py')'/Users/ethan/coding/python'>>> os.path.dirname('/Users/ethan/coding/python/')'/Users/ethan/coding/python'>>> os.path.dirname('/Users/ethan/coding/python')'/Users/ethan/coding'# 获取文件名或文件夹名>>> os.path.basename('/Users/ethan/coding/python/hello.py')'hello.py'>>> os.path.basename('/Users/ethan/coding/python/')''>>> os.path.basename('/Users/ethan/coding/python')'python'#os.path.splitext:分离文件名与扩展名>>> os.path.splitext('/Users/ethan/coding/python/hello.py')('/Users/ethan/coding/python/hello', '.py')>>> os.path.splitext('/Users/ethan/coding/python')('/Users/ethan/coding/python', '')>>> os.path.splitext('/Users/ethan/coding/python/')('/Users/ethan/coding/python/', '')#os.path.split:分离目录与文件名>>> os.path.split('/Users/ethan/coding/python/hello.py')('/Users/ethan/coding/python', 'hello.py')>>> os.path.split('/Users/ethan/coding/python/')('/Users/ethan/coding/python', '')>>> os.path.split('/Users/ethan/coding/python')('/Users/ethan/coding', 'python')
os.walk 是遍历目录常用的模块,它返回一个包含 3 个元素的元祖:(dirpath, dirnames, filenames)。dirpath 是以 string 字符串形式返回该目录下所有的绝对路径;dirnames 是以列表 list 形式返回每一个绝对路径下的文件夹名字;filesnames 是以列表 list 形式返回该路径下所有文件名字。
>>> for root, dirs, files in os.walk('/Users/ethan/coding'):... print root... print dirs... print files.../Users/ethan/coding['python'][]/Users/ethan/coding/python['web2']['hello.py']/Users/ethan/coding/python/web2[][]
0 0
- Python学习VI --- 文件与目录
- Linux 文件(vi/vim)与目录管理
- Python学习笔记(18)-目录操作与文件读写
- Python学习---文件目录操作
- python:目录与文件操作
- python目录与文件操作
- python:目录与文件操作
- python 文件与目录操作
- python 文件与目录操作
- Python 文件与目录操作
- Python 文件与目录操作
- python文件与目录操作
- Python 文件与目录操作 .
- Python目录与文件操作
- Python 文件与目录操作
- python文件与目录操作
- Python文件操作与目录
- Python学习系列(七)---遍历文件目录
- 蓝桥网 带分数(枚举/dfs)
- system用户登陆的时候提示 ora-28000/ora-28002
- 泛型的约束理解
- 获得本机ipv4和ipv6地址(即有线网卡地址)
- java代码简单操作es高亮
- Python学习VI --- 文件与目录
- 运行地址和加载地址
- 复习如何用循环输入数组
- Android消息处理机制_持续更新中
- 【典型例题】数据库——用MySQL来建立创建员工表;
- LEETCODE--Maximum Subarray
- 入门训练 圆的面积
- java中不同访问权限
- 使用Apache的FileUpLoad完成文件上传(源码+完整注释)