day 13 note 包和文件操作
01. 包的使用
1. 什么是包
包是python工程中一种专门用来管理py文件的文件夹,这个文件夹中有一个特殊文件: init.py
(项目中的普通文件夹一般用来管理,项目需要的非代码文件)
2. 怎么使用包中的内容 ---- > 导入
1) import 包名 - 导入后可以通过 ‘包名.’ 去使用这个包中的init.py文件中定义的所有的全局变量
2) import 包名.模块名 - 导入后可以通过 ‘包名.模块名.’ 去使用指定模块中所有的全局变量
3) from 包名 import 模块名 - 导入指定包中的指定模块,导入后可以通过 ‘模块名.’ 去使用模块中所有的全局变量
4) from 包名.模块名 import 变量1,变量2,… - 导入指定包中的指定模块中的指定变量, 变量在用的时候直接用
1)直接导入包
2)通过包直接导入模块
结合重命名导入模块
import fileManager.files as files, fileManager.jsonfile as js_file
files.read_file()
files.open_file()
print(files.f)
js_file.an_json()
3) 通过包直接导入模块
4) 导入指定包中的指定模块中的指定变量
02. 文件操作
1. 数据持久化
程序中使用和产生的数据默认都是保存在运行内存中的,当程序结束后,运行内存中的数据全部会被自动销毁;
如果想要数据在程序结束后不销毁,就需要将数据通过文件存储到硬盘中。
将数据保存到硬盘中,就是数据持久化。(注意:数据不能直接放到硬盘中,必须要通过文件)
编程的时候常见的文件类型: txt、json、plist、数据库
2. 文件操作
基本步骤: 打开文件 -> 操作文件(读操作、写操作)----> 关闭文件
1) 打开文件
open(file, mode=‘r’,*,encoding=None)
A. file - 需要打开的文件的路径
绝对路径: 文件在计算机中的全路径
相对路径:. - 表示当前目录(当前目录指的是当前写打开文件的代码的文件所在的目录)
… - 表示当前目录的上层目录
B. mode - 文件打开方式(决定打开文件后支持的操作是读还是写;决定操作的数据对象是字符串还是二进制)
(1)决定读写方式的值:r、w、a
r - 只读
w - 只写;打开后会清空原文件中的内容
a - 只写;打开后不会清空原文件中的内容
(2)决定操作的数据类型:t (默认值)、 b(二进制)
t - 读到的内容和写入文件中的内容是字符串类型
b - 读到的内容和写入文件中的内容是bytes(二进制)类型
注意:打开文件的时候mode必须在这两组值中每一组选一个,如果第二组的值不选表示选的 t
C. encoding - 文本文件编码方式,一般赋值为 ‘utf-8’ (读写时 编码方式要一致)
只有文本 文件在以 ‘t’ 的形式打开的时候才能设置encoding
**1.=================== 路 径 =========================**
1. 绝对路径
open(r'/Users/yuting/授课/Python2005/02语言基础/day13-包和文件操作/sources/aaa.txt')
2. 相对路径的 .
open('./sources/aaa.txt')
open('../day13-包和文件操作/sources/aaa.txt')
**2 ================= 打开方式 =========================**
f = open('./sources/aaa.txt', 'rt')
result = f.read()
f.write('abc') # 报错!not writable
print(type(result)) # <class 'str'>
f = open('./sources/aaa.txt', 'rb')
result = f.read()
print(type(result)) # <class 'bytes'>
f = open('./sources/aaa.txt', 'r')
result = f.read()
print(type(result)) # <class 'str'>
a - 支持写操作
f = open('./sources/aaa.txt', 'at')
result = f.read() # 报错! not readable
f.write('abc')
f = open('./sources/aaa.txt', 'ab')
f.write(bytes('abc', encoding='utf-8'))
f = open('./sources/aaa.txt', 'wt')
f.read() # not readable
f.write('hello')
03. 文件的读写
1. 打开文件
以读的形式打开一个不存在的文件,会报错!
f = open('sources/bbb.txt') # FileNotFoundError
以写的形式打开一个不存在的文件,不会报错,并且会自动创建这个不存在的文件
f = open('sources/ccc.txt', 'w', encoding='utf-8')
2. 关闭文件
1. 文件对象 . close( )
注意:文件关闭以后不能再对文件进行操作
f = open('./sources/aaa.txt', encoding='utf-8')
f.read()
f.close()
f.read() # ValueError: I/O operation on closed file.
2. 自动关闭文件的写法
with open( ) as 文件对象:
文件作用域
with open('./sources/aaa.txt', encoding='utf-8') as f:
f.read()
f.read() # ValueError: I/O operation on closed file.
3. 操作文件
(1) 读操作
a. 文件对象.read( ) - 从文件读写位置开始,读到文件结尾
with open('./sources/aaa.txt', encoding='utf-8') as f:
result = f.read( )
print('第一次读:\n', result)
f.seek(0) # 将读写位置移动到文件开头
result = f.read()
print('第二次读:\n', result)
二进制文件的读和写
with open('./sources/luffy.jpg', 'rb') as f:
result = f.read()
with open('./sources/b.jpg', 'wb') as f:
f.write(result)
b. 文件对象.readline( ) - 读一行(从读写位置开始,读到一行结束); 只能用于文本文件的读操作
with open('sources/aaa.txt', encoding='utf-8') as f:
result1 = f.readline()
print(result1)
result2 = f.readline()
print(f'result2:{result2}')
print('===================')
result3 = f.read()
print(result3)
练习:使用readline将文本文件中的内容一行一行的读,读完为止
with open('sources/bbb.txt', encoding='utf-8') as f:
while True:
result = f.readline()
print(result)
if not result:
break
c. 文件对象.readlines( )
with open('sources/aaa.txt', encoding='utf-8') as f:
result = f.readlines()
print(result) # ['床前明月光,\n', '疑是地上霜;\n', '举头望明月,\n', '低头思故乡。']
(2) 写操作
文件对象.write(数据)
a. 在文件最后追加新的内容
with open('sources/aaa.txt', 'a', encoding='utf-8') as f:
f.write('\n==================')
b.在文件最开头添加新的内容
print('---------------------------------------')
with open('sources/aaa.txt', encoding='utf-8') as f:
result = f.read()
print(result)
with open('sources/aaa.txt', 'w', encoding='utf-8') as f:
a.在文件开头添加一行内容
result = '======================\n'+result
f.write(result)
b.将原文件中的 '床' 修改成 'bed'
result = result.replace('床', 'bed')
f.write(result)
练习:删除原文件第3行的内容
with open('sources/aaa.txt', encoding='utf-8') as f:
result = f.readlines()
if len(result) >= 3:
del result[2]
new_content = ''.join(result)
with open('sources/aaa.txt', 'w', encoding='utf-8') as f:
f.write(new_content)