文件操作
open()
r 只读 r+ 可读可写 可指定位置写
w 只写 w+可读可写 先清空再写
x 文件存在, 报错; 不存在, 创建并写内容
a 追加 x+ 可读可写 在最后写
文件对象内部函数
read()
无参数, 读全部
参数: b按字节, 无b按字符
tell()
获取当前指针位置
seek()
调整指针到指定位置
write()
写数据, b: 写字节 无b: 写字符
fileno()
文件描述符
flush()
强刷, 保存缓存区数据到硬盘上
truncate()
截断, 指针为后的清空
close()
关闭文件
for循环文件对象
f = open(xxx)
for line in f:
print(line)
with操作文件
#打开一个文件, 操作文成后关闭
with open(xxx) as f:
pass
#同时打开两个文件
with open(xx1) as f1, open(xx2) as f2:
pass