前言
在学习文件操作之前,先回顾有关编码和数据类型的相关知识
-
字符串类型(str),在程序中用于表示文字信息,本质上是unicode编码中的二进制
例如:name = "卡竹兰" -
字节类型(bytes)
可以表示文字信息,实际上是utf-8/gbk等编码的二进制(对unicode进行压缩,方便文件存储和网络传输)
name = "卡竹兰"
data = name.encode('utf-8')
print(data) #b'\xe5\x8d\xa1\xe7\xab\xb9\xe5\x85\xb0'
result = data.decode('utf-8')
print(result) #卡竹兰
1.1读文件
- 读文本文件
打开文件
—路径:
相对路径:’info.txt‘
绝对路径:D:\python学习\文件操作\info.txt
在绝对路径最左边加r,防止转义字符出现的歧义“rD:\python学习\文件操作\info.txt”
—模式:
rb,表示读取文件原始的二进制(r读 read;b,二进制 binary)
#1.打开文件
file_object = open('info.txt', mode='rb')
file_object = open('rD:\python学习\文件操作\info.txt', mode='rb')
# 2.读取文件内容,并赋值给data
data = file_object.read()
# 3.关闭文件
file_object.close()
print(data)
- 读图片等非文本内容文件
file_object = open('a1.png', mode='rb') #打开
data = file_object.read() #读取
file_object.close() #关闭
print(data)
补充:
rt 模式读取文件
#1.打开文件(这里使用绝对路径)
file_object = open('rD:\python学习\文件操作\info.txt', mode='rt' , encoding = 'utf-8')
# 2.读取文件内容,并赋值给data
data = file_object.read() #读出来直接是文本
# 3.关闭文件
file_object.close()
print(data)
1.2写文件
与读文件思路相同:
- 打开文件
- 写文件
- 关闭文件
# 1.打开文件
file_object1 = open("t1.txt", mode='wb') # 写入内容时,需要字节类型
file_object2 = open("t1.txt", mode='wt', encoding='utf-8') # 写入内容时,需要字符类型
# 2.写入内容
file_object1.write('李云桐'.encode('utf-8'))
file_object2.write('李云桐')
# 3.关闭文件
file_object1.close()
file_object2.close()
1.3文件打开模式
"""
r open for reading
w open for writing
x create a new file and open it for writing
a open for writing , appending to the end of the file if it exists
b binary mode
t text mode
+ open a disk file updating(reading and writing)
"""
四种打开模式:
| r/rt ,rb | w/wt ,wb | x/xt ,xb | a/at ,ab | |
|---|---|---|---|---|
| 存在 | 读 | 清空再写 | 报错 | 尾部追加 |
| 不存在 | 报错 | 创建再写 | 创建再写 | 创建再写 |
读写配合:
| r+/rt+ ,rb+ | w+/wt+ ,wb+ | x+/xt+ ,xb+ | a+/at+ ,ab+ | |
|---|---|---|---|---|
| 光标位置 | 起始位置 | 起始位置(清空文件) | 起始位置(新文件) | 末尾 |
1.4常见功能
- 读n个字符(字节)
f = open('info.txt', mode='r', encoding='utf-8')
#读一个字符
data = f.read(1)
f.close()
print(data)
#读一个字节
f = open('info.txt', mode='rb')
data = f.read(1)
f.close()
print(data)
打开读字节,文本打开读字符。一个汉字一般是3个字节
- readline 读一行
f = open('info.txt', mode='rt', encoding='utf-8')
#读第一行
first_line = f.readline()
print(first_line, end='')
#继续读下一行
second_line = f.readline()
print(second_line)
#循环readline 读所有行-----------------------------
f = open('info.txt', mode='rt', encoding='utf-8')
for line in f:
print(line.strip())
f.close()
- readlines 读所有行
#readlines 读所有行-----------------------------
f = open('info.txt', mode='rt',encoding='utf-8')
#读第一行
data_list = f.readlines()
print(data_list)
1.5写和光标的相关功能
- flush函数
文件读写时,不是写到了硬盘,而是写在缓冲区,系统会将缓冲区的内容刷到硬盘
使用.flush()函数可以手动将内容刷到硬盘
f = open('info.txt', mode='a', encoding='utf-8')
f.write("李云桐")
f.flush() # 立即将缓冲区所有数据,刷到硬盘
f.close()
- 移动光标
f = open("info.txt", mode='r+', encoding='utf-8')
f.seek(3) # 移动到指定字节位置
# 注意:此方法对于 mode='a+'无效,因为a模式总是先把光标放到最后,再进行写入
f.write("李云桐")
f.close()
移动光标位置,在此光标之后开始写内容(如果原来有内容,会被覆盖
- 获取光标位置
f = open("info.txt", mode='r+', encoding='utf-8')
p1 = f.tell() # 返回的是字节的位置,而不是字符的位置
print(p1)
f.read(3)
print(p1)
p2 = f.tell()
print(p2) # 9
f.close()
只能返回字节位置,而不是字符位置
1.6上下文管理
之前对文件进行操作时,每次都要打开和关闭文件,比较繁琐且容易忘记关闭文件
以后操作文件使用with...as..,使用上下文管理,实现自动关闭文件
with open("info.txt", mode='rb') as f:
data = f.read()
print(data)
python 2.7之后,with又同时支持多个文件进行上下文管理:
with open('xxx' xxx) as f1,open('xxx' xxx) as f2:
pass

被折叠的 条评论
为什么被折叠?



