python file io
文章目录
open mode
- 'w'
- 'r'
'a'
- append 模式
- 注意:
文件不存在,不会报错: 1. 创建文件 2. 写入文件
行分割符 linesep 的处理
read write 的默认转换
原则:在以 text ( "r" ) 模式打开文件时,python 会自动把 os.linesep 替换成 ~"\n"~。
即不论文件中存储的是 "\r"(macos)、 "\r\n"(windows) 或者 "\n" 在当前系统上, python 的 read 都会把它替换成"\n"; python write 会做逆向转换
关于 file.seek() 操作
注意 os.linesep 处理陷阱:
- os.read 读取到的是一个字符
"\n", 但是文件指针(offset)跳转的却是len(os.linesep) 因此,如果读取 "\n" 后,想要跳转回原来的位置需要跳转
len(os.linesep)个偏移量1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16import os with open(file, "r+") as f: ... pos = f.tell() ch = f.read(1) # 假设读取到的是“\n” # 现在的位置是 pos + len(os.linesep) # 跳转回“\n” 之前的正确方法 f.seek(f.tell()-len(os.linesep), os.SEEK_SET) # 或者下面的方法 f.seek(pos, os.SEEK_SET) # 错误方法 f.seek(f.tell() - 1, os.SEEK_SET) # windows 上偏移量计算错误 f.seek(-len(os.linesep), os.SEEK_CUR) # 验证报错
write read 和 seek 偏移量的差异
- f.seek 总是以 1 byte 为偏移量单位
f.read 和 f.write
在 text mode 读写是,以 1 个字符为偏移量单位
- 注意可能是多个 byte, 对于
os.linesep或者多字节字符
- 注意可能是多个 byte, 对于
- 在 byte mode 读写时,以 1 个 byte 为偏移量单位
删除文件末尾换行符 os.linesep 的方法
| |
修改最好一行的方法
| |
文本编辑器最好一个换行 os.linesep 表示
vscode
末尾没有 os.linesep
1 this is line
- windows 上:
"this is line"
- windows 上:
末尾有 os.linesep
1 this is line 2
- windows 上:
"this is line\r\n"
- windows 上:
注意:
- 通过
有行号的空行标志 os.linesep - 光标可移动位置不能说明问题
- 通过
emacs
末尾没有 os.linesep
1 this is line
末尾有 os.linesep
1 this is line
windows 上:
"this is line\r\n"- 一个 os.linesep
1 this is line 2
windows 上:
"this is line\r\n\r\n"- 两个 os.linesep
注意:
- 通过
光标可移动的空行数标志有几个 os.linesep
- 通过
TODO 多字节字符的读写 f.read() f.write()
文章作者
上次更新 2024-01-05 (5c92d1c)