Python学习笔记 - 文件相关操作
创建文件对象
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
打开一个文件并返回此文件对象。
file 参数应该传入一个字符串,此字符串包含了文件路径
mode 参数应该传入一个字符串,此字符串说明了打开文件的模式
r:只读模式(默认)w:写模式,打开时若文件存在会先清空文件内容不存在则创建新文件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)
- ‘+’ :为了更新而打开一个文件
- ‘U’ :universal newlines mode (deprecated)
读文件
.read([<int>]).readline().readlines()
写文件
.write(<str>)
遍历文件
移动指针
.tell():返回文件指针位置.seek(<int>):传入一个位置来跳转,并返回这个位置