文件操作
-
文件的读写模式
""" 只读模式(r),默认使用只读模式 使用只读模式只能读取文件 """ # 使用方法 with open('a.txt', 'r', encoding='utf8') as file: pass # 如果路径文件不存在,则直接报错 """ 只写模式(w) 使用只写模式打开的文件只能写入内容 """ # 使用方法 with open('a.txt', 'w', encoding='utf8') as file: pass # 如果路径文件不存在,则直接创建新文件 # 如果路径文件存在,则会清空该文件 """ 只追加模式(a) 只能在文件最后追加内容 """ # 使用方法 with open('a.txt', 'a', encoding='utf8') as file: pass # 如果路径文件不存在,则直接创建新文件 # 如果路径文件存在,则直接在文件末尾准备追加内容,不会清空
-
文件的操作模式
-
文本模式(t)
- 文件的操作模式默认使用的是文本模式,所以可以忽略不写
r、w、a就等同于rt、wt、at - 文本模式只能操作文本文件
- 需要指定encoding参数
- 以字符串为单位
- 文件的操作模式默认使用的是文本模式,所以可以忽略不写
-
二进制模式(b)
- 参数rb、wb、ab要完整填入
- 能够操作所有类型文件
- 因为是二进制,所以不需要encoding参数
- 以bytes为单位
读写模式 说明 r 只读 w 只写 a 追加 rb 以二进制模式只读 wb 以二进制模式只写 ab 以二进制模式追加
-
-
文件的方法
with open('a.txt', 'r', encoding='utf8') as file: """read() 读取文件内容,读取完毕光标停留在最后""" file.read() """readline() 读取文件一行内容""" file.readline() # 更建议采用for循环来读取文件内容 for f in file: pass """readlines() 把内容以一行的方式组织成列表返回""" file.readlines() """readable() 判断当前文件是否支持读取""" file.readable() with open('a.txt', 'b', encoding='utf8') as file: """write() 写入数据到文件中""" file.write() """writelines() 将容器类型里多个数据值写入到文件中""" file.writelines() """writeable() 判断当前文件是否支持写入""" file.writeable() """flush() 将文件数据保存到硬盘""" file.flush()
文件方法 说明信息 read 读取文件信息 readline 读取文件一行信息 readlines 将文件所有信息整理成一行列表返回 readable 判断当前文件能否读取 write 写入信息到文件 writelines 将容器类型的数据一一写入到文件中 flush 将内存中的文件数据保存到硬盘中
练习
# 1.
# 编写简易版本的拷贝工具
# 自己输入想要拷贝的数据路径
# 自己输入拷贝到哪个地方的目标路径
# 任何类型数据皆可拷贝
# ps: 个别电脑C盘文件由于权限问题可能无法拷贝
# 换其他盘尝试即可
copy_path = input('请输入需要拷贝的文件路径(包含文件名):')
paste_path = input('请输入需要存放的文件路径(包含文件名):')
with open(fr'{copy_path}', 'r', encoding='utf8') as old_file, open(fr'{paste_path}', 'w', encoding='utf8') as new_file:
for file in old_file:
new_file.write(file)
# 2.
# 利用文件充当数据库编写用户登录、注册功能
# 文件名称: userinfo.txt
# 基础要求:
# 用户注册功能 >> >: 文件内添加用户数据(用户名、密码等)
# 用户登录功能 >> >: 读取文件内用户数据做校验
# ps: 上述功能只需要实现一次就算过关(单用户)
# 文件内始终就一个用户信息
# 拔高要求:
# 用户可以连续注册
# 用户可以多账号切换登录(多用户)
# 文件内有多个用户信息
# ps: 思考多用户数据情况下如何组织文件内数据结构较为简单
# 提示: 本质其实就是昨天作业的第二道题
# 只不过数据库由数据类型变成文件
with open(r'userinfo.txt', 'a', encoding='utf8') as f:
pass
while True:
choice = input("""
--------------------------
1.用户登录
2.新用户注册
--------------------------
请输入:
""").strip()
if choice == '1':
true = 1
while true:
name = input('用户名:').strip()
pwd = input('密码:').strip()
with open(r'userinfo.txt', 'r', encoding='utf8') as file:
for f in file:
if f.split(',')[0] == name:
if f.split(',')[1] == pwd:
while True:
option = input("""
----------------------
登录成功:
1.切换用户
2.返回主菜单
----------------------
""").strip()
if option == '1':
break
elif option == '2':
true = 0
break
break
else:
print('密码错误')
break
else:
print('用户名错误')
exit_while = input('退出输入exit:')
if exit_while == 'exit':
break
continue
elif choice == '2':
while True:
user_name = input('请输入用户名:').strip()
password = input('请输入密码').strip()
with open(r'userinfo.txt', 'r', encoding='utf8') as read_file:
for f in read_file:
if f.split(',')[0] == user_name:
print('用户名已存在')
break
else:
info = ",".join([user_name, password])
with open(r'userinfo.txt', 'a', encoding='utf8') as write_file:
write_file.write(info)
write_file.write(',\n')
again = input("是否继续注册(继续注册输入y)").strip().lower()
if again == 'y':
continue
else:
break
else:
print('请输入正确的选项')