Python之第八天的努力--文件操作
01 内容回顾
-
数据类型的补充
- str:
- tuple:
- ('hhh')--->str
- count index
- list
- sort sort(reverse=True) reverse()
- 列表相加 列表与数字相乘
- 循环列表的问题
- dict
- update 更新,增加值,修改值,创建字典,将一个字典的所有键值对覆盖添加到另一个字典
- dict.fromkeys(iterable,value)
- 循环字典。
- 数据类型的转换:0,{},[],set(),'',None---->bool---->False
-
编码的进阶:
ASCLL,gbk,Unicode,utf-8......
-
所有的编码本(除去Unicode之外)不能直接互相识别。
-
在内存中所有的数据必须是Unicode编码存在,出去bytes。
int
bool
tuple str bytes
list
dict
set
-
str bytes
-
称呼: 文字版本 字节文本
'' "" """ b'' b""
Unicode 非Unicode
02 文件操作
-
文件操作的初识
- 歌单.txt
- 利用Python代码写一个很low的软件,去操作文件。
- 文件路径:path
- 打开方式:读, 写, 追加, 读写, 写读.....
- 编码方式:utf-8, gbk, gb2312......
- 报错原因
- UnicodeDecodeError:文件存储时与文件打开时编码本运用不一致。
- 路径分隔符: r'路径' r'd:\歌单.txt'
-
文件操作的读
r, rb, r+,r+b.... 四种模式
r: read()** ,read(n),readline(),readlines()
for ***
# read 全部读出来 # f = open('文件的读',encoding='utf-8',mode='r') # content = f.read() # print(content,type(content)) # <class 'str'> # f.close() # read(n) 按字符读取 # f = open('文件的读',encoding='utf-8',mode='r') # content = f.read(5) # print(content) # f.close() # readline() # f = open('文件的读',encoding='utf-8',mode='r') # # content = f.readline() # print(f.readline()) # print(f.readline()) # f.close() # readlines() 返回一个列表,列表中的每个元素是源代码的每一行。 # f = open('文件的读',encoding='utf-8',mode='r') # l1 = f.readlines() # print(l1) # f.close() # for 读取 # 类比于['最爱小疯子\n', 'I want know oh,I want know ohh~'] # f = open('文件的读',encoding='utf-8',mode='r') # for line in f: # print(line) # f.close()
rb:操作的是非文本的文件。图片,视频,音频。
-
文件操作的写
w,wb,w+,w+b 四种模式
# 没有文件,创建文件,写入内容 # f = open('文件的写',encoding='utf-8',mode='w') # f.write('随便写一点') # f.close() # 如果文件存在,先清空原文件内容,在写入新内容 # f = open('文件的写',encoding='utf-8',mode='w') # f.write('哈哈哈哈哈') # f.close() # wb # f = open('好想爱这个世界啊.jpg',mode='rb') # content = f.read() # # print(content) # f.close() # # f = open('好想爱这个世界啊2.jpg',mode='wb') # f.write(content) # f.close() -
文件操作的追加
a,ab,a+,a+b 四种模式
# 没有文件创建文件,追加内容 # f = open('文件的追加',encoding='utf-8',mode='a') # f.write('哈哈哈哈哈哈哈哈哈') # f.close() # 有文件,在原文件的最后面追加内容 # f = open('文件的追加',encoding='utf-8',mode='a') # f.write('66666,牛批牛批') # f.close() -
文件操作的其他模式
r+
# 读并追加 f = open('文件的读写',encoding='utf-8',mode='r+') content = f.read() print(content) f.write('牛批牛批') f.close() -
文件操作的其他功能
总结:
三个大方向:读, 四种模式:r, rb, r+,r+b
写, 四种模式:w,wb,w+,w+b
追加,四种模式:a,ab,a+,a+b
相应功能:对文件句柄的操作:read read(n) readline() readlines() write()
tell() seek() flush()
# tell 获取光标的位置 单位是字节 # f = open('文件的读写',encoding='utf-8') # print(f.tell()) # content = f.read() # print(content) # print(f.tell()) # f.close() # seek 调整光标的位置 # f = open('文件的读写',encoding='utf-8') # f.seek(9) # content = f.read() # print(content) # f.close() # flush 强制刷新 # f = open('文件的其他功能',encoding='utf-8',mode='w') # f.write('hhhhhhhhhhhhhh') # f.flush() # f.close() -
打开文件的另一种方式
# 优点1:不用手动关闭文件句柄 # with open('文件的读',encoding='utf-8') as f1: # print(f1.read()) # 优点2: with open('文件的读',encoding='utf-8') as f1,\ open('文件的写',encoding='utf-8',mode='w') as f2: print(f1.read()) f2.write('ttttttttttt') # 缺点:待续 -
文件的改的操作
''' 1.以读的模式打开原文件。 2.以写的模式创建一个新的文件。 3.将原文件的内容读出来修改成新内容,写入新文件。 4.将原文件删除。 5.将新文件重命名成原文件。 ''' # low版 # import os # # 1.以读的模式打开原文件。 # # 2.以写的模式创建一个新的文件。 # with open('华晨宇',encoding='utf-8') as f1,\ # open('华晨宇.bak',encoding='utf-8',mode='w') as f2: # # 3.将原文件的内容读出来修改成新内容,写入新文件。 # old_content = f1.read() # new_content = old_content.replace('华晨宇','他') # f2.write(new_content) # os.remove('华晨宇') # os.rename('华晨宇.bak','华晨宇') # 进阶版: import os # 1.以读的模式打开原文件。 # 2.以写的模式创建一个新的文件。 with open('华晨宇',encoding='utf-8') as f1,\ open('华晨宇.bak',encoding='utf-8',mode='w') as f2: # 3.将原文件的内容读出来修改成新内容,写入新文件。 for line in f1: new_line = line.replace('他','华晨宇') f2.write(new_line) os.remove('华晨宇') os.rename('华晨宇.bak','华晨宇'

浙公网安备 33010602011771号