一、文件的打开与关闭
- 打开文件/创建文件
在python,使用open函数,可以打开一个已经存在的文件,或者创建一个新文件
open(文件路径,访问模式)
f = open('test.txt', 'w')
# 打开文件
fp = open('test.txt', 'w')
fp.write('hello world')
# 文件夹不可以创建,暂时需要手动创建
fp = open('demon/text.txt', 'w')
fp.write('hello world')
文件路径:
-
绝对路径:指的是绝对位置,完整地描述了目标的所在地,所有目录层级关系是一目了然的。
- 例如: E:\python,从电脑的盘符开始,表示的就是一个绝对路径。
-
相对路径:是从当前文件所在的文件夹开始的路径。
- test.txt ,是在当前文件夹查找 test.txt 文件
- ./test.txt ,也是在当前文件夹里查找 test.txt 文件, ./ 表示的是当前文件夹。
- ../test.txt ,从当前文件夹的上一级文件夹里查找 test.txt 文件。 ../ 表示的是上一级文件夹
- demo/test.txt ,在当前文件夹里查找 demo 这个文件夹,并在这个文件夹里查找 test.txt 文件。
访问模式:
访问模式 | 说明 |
---|---|
r | 以只读方式打开文件。文件的指针将会放在文件的开头。如果文件不存在,则报错。这是默认模式。 |
w | 打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。 |
a | 打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将 会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。 |
r+ | 打开一个文件用于读写。文件指针将会放在文件的开头。 |
w+ | 打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。 |
a+ | 打开一个文件用于读写。如果该文件已存在,文件指针将会放在文件的结尾。文件打开时会是追加模 式。如果该文件不存在,创建新文件用于读写。 |
rb | 以二进制格式打开一个文件用于只读。文件指针将会放在文件的开头。 |
wb | 以二进制格式打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新 文件。 |
ab | 以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是 说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。 |
rb+ | 以二进制格式打开一个文件用于读写。文件指针将会放在文件的开头。 |
wb+ | 以二进制格式打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文 件。 |
ab+ | 以二进制格式打开一个文件用于读写。如果该文件已存在,文件指针将会放在文件的结尾。如果该文 件不存在,创建新文件用于读写。 |
- 关闭文件
# 文件的关闭
fp = open('a.txt', 'w')
fp.write('hello')
fp.close()
二、文件的读写
- 写数据
# write方法
fp = open('test.txt', 'w')
fp.write('hello world, i am here\n' * 5)
fp.close()
# 如果文件存在,会先清空原来的数据,然后再写
fp = open('test.txt', 'a')
fp.write('hello world, i am here\n' * 5)
fp.close()
# 我想在每一次执行之后都要追加数据 a
# 如果模式变为a,那么就会执行追加的操作
-
读数据
- 默认情况下,read是一字节一字节的读,效率比较低
fp = open('test.txt', 'r') content = fp.read() print(content)
hello world, i am here hello world, i am here hello world, i am here hello world, i am here hello world, i am here hello world, i am here hello world, i am here hello world, i am here hello world, i am here hello world, i am here
- readline是一行一行的读取,但是只能读取一行
fp = open('test.txt', 'r') content = fp.readline() print(content)
hello world, i am here
- readlines可以按照行来读取,但是会将所有的数据都读取到,并且以一个列表的形式返回,而列表的元素,是一行一行的数据
fp = open('test.txt', 'r') content = fp.readlines() print(content)
['hello world, i am here\n', 'hello world, i am here\n', 'hello world, i am here\n', 'hello world, i am here\n', 'hello world, i am here\n', 'hello world, i am here\n', 'hello world, i am here\n', 'hello world, i am here\n', 'hello world, i am here\n', 'hello world, i am here\n']
三、序列化和反序列化
通过文件操作,我们可以将字符串写入到一个本地文件。但是,如果是一个对象(例如列表、字典、元组等),就无 法直接写入到一个文件里,需要对这个对象进行序列化,然后才能写入到文件里。
设计一套协议,按照某种规则,把内存中的数据转换为字节序列,保存到文件,这就是序列化,反之,从文件的字 节序列恢复到内存中,就是反序列化。
对象 ---> 字节序列 === 序列化
字节序列 ---> 对象 === 反序列化
Python中提供了JSON这个模块用来实现数据的序列化和反序列化。
fp = open('test.txt', 'w')
# 默认情况下,我们只能将字符串写入到文件中
fp.write('hello world')
fp.close()
fp = open('test.txt', 'w')
# 默认情况下,对象是无法写入到文件中,如果想写入到文件中,那么必须使用序列化操作
name_list = ['zhangsan', 'lisi']
fp.write(name_list)
序列化
- dumps()
# (1) 创建一个文件
fp = open('test.txt', 'w')
# (2) 定义一个列表
name_list = ['zs', 'ls']
# 导入json模块到该文件中
import json
# 序列化
# 将python对象 变成 json字符串
# 我们在使用scrapy框架的时候,该框架会返回一个对象,我们要将对象写入到文件中,就要使用json.dumps
names = json.dumps(name_list)
print(names) # ["zs", "ls"]
print(type(names)) # <class 'str'>
# 将names写入到文件中
fp.write(names)
fp.close()
- dump
# 将对象转换为字符串的同时,指定一个文件的对象,然后将转换后的字符串写入到这个文件中
fp = open('test.txt', 'w')
name_list = ['zs', 'ls']
import json
json.dump(name_list, fp)
fp.close()
反序列化
# 将json字符串变成一个python对象
fp = open('test.txt', 'r')
content = fp.read()
print(content) # ["zs", "ls"]
print(type(content)) # <class 'str'>
- loads
import json
# 将json字符串变成python对象
result = json.loads(content)
print(result) # ['zs', 'ls']
print(type(result)) # <class 'list'>
- load
fp = open('test.txt', 'r')
import json
result = json.load(fp)
print(result) # ['zs', 'ls']
print(type(result)) # <class 'list'>