python第八课

# f=open('f:\模特主妇护士班主任.txt',mode='r',encoding='utf-8')
# content=f.read()
# print(content)
# f.close()

#对于w没有此文件就去创建文件
# f=open('log',mode='w',encoding='utf-8')
# content=f.write('今晚做爱')
# print(content)
# f.close()
# #先将源文件内容全部清除,在写
# f=open('log',mode='w',encoding='utf-8')
# content=f.write('我爱你启丽')
# print(content)
# f.close()
# f=open('log',mode='wb')
# content=f.write('我非常爱你启丽'.encode('utf-8'))
# print(content)
# f.close()
#追加
# f=open('log',mode='a',encoding='utf-8')
# f.write('今晚做爱吧!')
# f.close()
#读写
# f=open('log',mode='r+',encoding='utf-8')
# print(f.read())
# f.write('小丽,爱你')
# f.close()

# f=open('log',mode='r+b')
# print(f.read())
# f.write('小丽,爱你'.encode('utf-8'))
# f.close()

# f=open('log',mode='w+',encoding='utf-8')
# f.write('我想你启丽')
# f.seek(0)
# print(f.read())
# f.close()
# f=open('log',mode='w+b')
# f.write('我想你启丽'.encode('utf-8'))
# f.seek(0)
# print(f.read())
# f.close()

# f=open('log',mode='a+',encoding='utf-8')
# f.write('我们做爱吧!')
# f.seek(0)
# print(f.read())
# f.close()
#功能详解
f=open('log',mode='r+',encoding='utf-8')
#content=f.read(12)#读出来的都是字符
# f.seek(10)#是按照字节定光标的位置
# f.tell()#告诉你光标的位置
# content=f.read()
# print(content)
# f.tell()
# f.readline()#是否刻度
# line=f.readline()#一行一行的读
#line=f.readlines()#每一行当成列表中的一个元素,添加到list中
# f.truncate(5)#截取
# for line in f:
# print(line)
# f.close()
#
# with open('log',mode='r+',encoding='utf-8') as obj,open('log',mode='w+',encoding='utf-8') as f1:
# print(obj.read())

username=input('请输入你要注册的用户:')
password=input('请输入你要注册的密码:')
with open('log',mode='w',encoding='utf-8') as f:
f.write('{}\n{}'.format(username,password))
print('注册成功')
lis=[]
i=0
while i< 3:
usn=input('请输入你的用户名:')
pwd=input('请输入你的密码:')
with open('log', mode='r+', encoding='utf-8') as f1:
for line in f1:
lis.append(line)
if usn==lis[0].strip() and pwd==lis[1].strip():
print('登陆成功')
break
else:print('帐号和密码错误')
i+=1

#文件处理
#打开文件
#open('路径','打开方式','指定编码方式')
#打开方式r w a r+ w+ a+
#r+打开文件直接写和读完再写
#编码方式--utf-8
#操作文件
#读
# read 一次尾读
# readline 一行一行读
#不知道在哪结束
#视频 图片 rb bytes 按照字节读
#for循环---最好
#写 write
# 光标----文件指针
#seek 指定光标移动到某个位置
#tell 截取光标当前的位置
#truncate 截取文件
#关闭文件 close
#修改文件
#文件是不能修改的
with open('log',encoding='utf-8') as f,open('log2','w',encoding='utf-8') as f2:
for line in f:
if '龚启丽' in line:
line=line.replace('龚启丽','启丽')
#写文件
f2.write(line)
#删除文件和重命文件
import os
os.remove('log')#删除文件
os.rename('log','log2')#重命名文件

猜你喜欢

转载自www.cnblogs.com/huangjianfeng/p/11223664.html