1.文件操作
1.文件操作
模特主妇护士老师.txt
1.文件路径:d:\ 模特主妇护士老师.txt
2.编码方式:utf-8
3.操作方式:只读,只写,追加,读写,写读
以什么编码方式储存的,就必须以什么编码方式打开
只读:#bytes——>str
# f=open('gcs',mode = 'r',encoding = 'utf-8')
# content = f.read()
# print(content)
# f.close()
#rb 是bytes(非文字文件)
f=open('gcs',mode = 'rb')
content = f.read()
print(content,type(content))
f.close()
#只写:
#对于写,没有此文件就会创建文件
# f=open('log',mode = 'w',encoding = 'utf-8')
# f.write('秀秀秀秀')
# f.close()
#先将源文件的内容全部清除,在写
# f=open('log',mode = 'w',encoding = 'utf-8')
# f.write('秀顺')
# f.close()
#wb bytes
# f=open('log',mode = 'wb')
# f.write('秀顺'.encode('utf-8'))
# f.close()
#追加:
# f=open('log',mode = 'a',encoding='utf-8')
# f.write('在一起')
# f.close()
f=open('log',mode = 'ab')
f.write('可以吗'.encode('utf-8'))
f.close()
#读写,先读出内容,在写入
# f= open('log',mode='r+',encoding='utf-8')
# print(f.read())
# f.write('祝您们幸福')
# f.close()
#读写的 bytes
f= open('log',mode='r+b')
print(f.read())
f.write(''.encode('utf-8'))
f.close()
#功能详解
f=open('gcs',mode = 'r+',encoding = 'utf-8')
# content = f.read(3)#读出来的都是字符
# f.seek(3)#按字节定光标的位置
#f.tell() #告诉你光标位置
# f.readable()#可读的
# line=f.readline()#一行一行的读
# line=f.readlines()#每一行当成列表中的一个元素,添加到列表list
# f.truncate(3)#在源文件截取
# print(line)
# for line in f:
# print(line)
# f.close()
# count=f.tell()
# f.seek(count-9)
# content = f.read(2)
# print(content)
# f.close()
# with open('gcs',mode = 'r+',encoding = 'utf-8') as obj:
# # obj.read()
# print(obj.read())
with open('gcs', mode='r+', encoding='utf-8') as obj,\
open('log', mode='r+', encoding='utf-8') as f1:
# obj.read()
print(obj.read())
print(f1.read())
2.编码
#str --->byte encode 编码
# s = '二哥'
# b = s.encode('utf-8')
# print(b)
# #byte --->str decode 解码
# s1 = b.decode('utf-8')
# print(s1)
# s = 'abf'
# b = s.encode('utf-8')
# print(b)
# #byte --->str decode 解码
# s1 = b.decode('gbk')
# print(s1)
3.注册登录
username = input('请输入账户名:')
passward = input('请输入密码:')
with open('f1', mode='w', encoding='utf-8') as obj:
obj.write('{}\n{}'.format(username,passward))
print('恭喜您,注册成功!')
lis = []
i = 0
while i < 3:
usn = input("请输入用户名:")
pwd = input('请输入密码:')
with open('f1', mode='r+', encoding='utf-8') as obj:
for line in obj:
lis.append(line)
if usn == lis[0].strip() and pwd == lis[1].strip():
print('登录成功!')
break
else:
print('账号和密码错误!')
i+=1