Day13 文件的读写基本操作
文章目录
1、文件读写模式
with 支持一次性打开多个文件
with open(r'user.txt', 'a', encoding='utf-8')as user_1, open(r'user.txt', 'r', encoding='utf')as user_read:
r 只读模式
使用该模式打开的文件只能读取内容,不能做其他操作
with open(r'user.txt','r',encoding='utf_8')as read_1:
- 当文件路径不存在会直接报错
w 只写模式
使用该模式打开的文件只能写内容,并且会清空文件内容,文件若不存在会创建文件 不能做其他操作
with open(r'user.txt','w',encoding='utf_8')as read_1:
a 只写模式
使用该模式打开文件只会在文件内容末尾添加内容,若文件不存在也会自动创建文件
with open(r'user.txt','a',encoding='utf_8')as read_1:
2、文件操作模式
2.1、文本模式 t
- 只能够操作文本文件
- 必须要指定encoding的参数
- 读写都是以字符串为单位
关键字 功能 rt 只读模式 wt 创建文件,重置内容,只写模式 at 创建文件,只写模式
2.2、二进制模式 b
- 能够操作任何文件 包括文本文件
- 不需要指定encoding
- 读写都是以bytes
关键字 功能 rb 只读模式 wb 创建文件,重置内容,只写模式 ab 创建文件,只写模式
2.3、模式+
- r+
打开文件进行读写,;若文件不存在,则会报错
with open(r'a.txt','a+',encoding='utf')as t1: t1.read() t1.write('\n哈哈哈')
- w+
打开一个文件用于读写。如果该文件已存在则打开文件,并从开头开始编辑,即原有内容会被删除。如果该文件不存在,创建新文件
with open(r'a.txt','w+',encoding='utf')as t1: t1.read() t1.write('\n哈哈哈')
- a+
打开一个文件用于读写。如果该文件已存在,文件指针将会放在文件的结尾。文件打开时会是追加模式。如果该文件不存在,创建新文件用于读写。
with open(r'a.txt','a+',encoding='utf')as t1: t1.read() t1.write('\n哈哈哈') >>> 哈哈哈
- x
创建一个新文件并打开它进行写入,如何文件已经存在则无法创建文件和写入内容
with open(r'a.txt','x',encoding='utf')as item: item.write() !!!文件已存在直接报错
3、文件诸多操作方法
读
- read: 一次性读取文件内容并且将光标移到末尾,继续读则为空
不推荐使用,当文件较大时占内存
with open(r'user.txt','r',encoding='utf_8')as read_1: print(read_1.read()) >>> ll|321 123|123 gg|520 56|32 64|12
- readline():一次只是读写一行内容,
with open(r'user.txt','r',encoding='utf_8')as read_1: print(read_1.readline()) print(read_1.readline()) print(read_1.readline()) >>> ll|321 123|123 gg|520
- readlines():按照一行行的方式读取所有内容 并装入列表输出
with open(r'user.txt','r',encoding='utf_8')as read_1: print(read_1.readlines()) >>>['kk|123\n', 'jack|123\n', 'll|321\n', '123|123\n', 'gg|520\n', '56|32\n', '64|12\n']
- 最佳的 读 方式是利用for循环将文件里的内容一条条赋值读出,这样每打印一条 那上一条的赋值就解绑 节省内存资源
with open(r'user.txt','a',encoding='utf_8')as write_1,open(r'user.txt','r',encoding='utf_8')as read_1: for i in read_1: i=i.strip() #通过删除首尾空格 来取消输出时中间空行 print(i) >>> ll|321 123|123 gg|520 56|32 64|12
写
- write():填写文件内容
with open(r'user.txt','a',encoding='utf_8')as write_1: write_1.write()
- writelines():支持填写容器类型(内部 可以存放多个数据值的数据类型)多个数据值
with open(r'user.txt','a',encoding='utf_8')as write_1: list_1=['542','tt'] write_1.writelines(dict_1)
- flush()将内存的文件数据立刻刷到硬盘(主动按ctrl+s)
with open(r'user.txt','a',encoding='utf_8')as write_1: write_1.flush()
作业
1.编写简易版本的拷贝工具
自己输入想要拷贝的数据路径 自己输入拷贝到哪个地方的目标路径
任何类型数据皆可拷贝
# 功能大循环
while True:
# 接受用户 输入的地址
user_path=input('请输入要拷贝的文件:').strip()
new_user_path=input('请输入需要拷贝目的的地址').strip()
# 文件操作 创建读 和写
with open(r'%s'%user_path,'rb')as user_read,open(r'%s'%new_user_path,'wb')as user_write:
for data in user_read:
user_write.write(data)
print('拷贝完成')
if input('继续拷贝请按’1,结束拷贝请按0:')==1:
continue
else:
print('程序结束')
break
2、利用文件充当数据库编写用户登录、注册功能
文件名称:userinfo.txt
# 系统功能大循环
while True:
# 功能选项输入
item = input('1.注册\n2.登录\n0.退出\n请输入:')
# 文件操作 只会用到读写:
with open(r'userinfo.txt', 'a', encoding='utf-8')as user_1, open(r'userinfo.txt', 'r', encoding='utf')as user_read:
# 注册功能
if item == '1':
print('账户注册'.center(40, '-'))
while True:
user_name = input('请输入您的账号:').strip()
# 读出文件内容
for user_str in user_read:
# 切割字符串装入列表,这里只判断账号 不需要清除尾部换行空格
user_list = user_str.split('|')
if user_name == user_list[0]:
print('账号已存在')
break
else:
user_password = input('请输入您的密码').strip()
login_user = '|'.join((user_name, user_password))
# 写入 账户字符串
user_1.write(f'{login_user}\n')
print(user_read.read())
print('注册成功')
break
continue
# 登录功能
elif item == '2':
print('用户登录'.center(40, '-'))
while True:
user_name = input('请输入用户名:')
user_password = input('请输入密码:')
for i in user_read:
# 因为写入时加入了换行符 这里直接先清除空格
user_str = i.strip()
# 切割装表
user_list = user_str.split('|')
if user_name == user_list[0]:
if user_password == user_list[1]:
print('登录成功')
break
else:
print('账号错误,请重试')
continue
break
# 退出系统
elif item == '0':
print('退出成功')
# 防呆设置
else:
print('无此选项')