文件操作


# 文件处理
    # 打开文件
        #open('路径','打开方式','指定编码方式')
        # 打开方式 r w a r+ w+ a+ b
            #r+ 打开文件直接写 和读完再写
        # 编码方式 —— utf-8
    # 操作文件
        #
            # read 一次性读
            # readlines 一次性读
            # readline 一行一行读
                #不知道在哪儿结束
                #视频 图片 rb bytes 按照字节读
            # for循环 —— 最好!!!
        #
            # write
        # 光标 —— 文件指针
            #seek _ 指定光标移动到某个位置
            #tell _ 获取光标当前的位置
            #truncate _ 截取文件
    # 关闭文件
        #close

# windows默认是GBK,python默认是utf-8
# 修改文件
with open('小护士班主任',encoding='utf-8') as f,open('小护士班主任.bak','w',encoding='utf-8') as f2:
    for line in f:
        if '星儿' in line:  #班主任:星儿
            line = line.replace('星儿','啊娇')
        #写文件
        f2.write(line) #小护士:金老板

import os
os.remove('小护士班主任') #删除文件
os.rename('小护士班主任.bak','小护士班主任')  #重命名文件

 

# 遇到\t的时候再前面加上\
# 用什么方式存储的就用什么方式打开
 f = open('d:\python1\\test.txt',mode='r',encoding='utf-8')
 content = f.read()
 print(content)
 f.close()

# 非文字类的打开用 rb,如图片
 f = open('d:\python1\\test.txt',mode='rb')
 content = f.read()
 print(content)
 f.close()


# python 是uncode

# 对于写,没有文件就创建文件,如果有覆盖
 f = open('d:\python1\\test2.txt',mode='w',encoding='utf-8')
 s = input('写点吧:')
 f.write(s)
 f.close()

# 以byts类型 。
 f = open('d:\python1\\test3.txt',mode='wb')
 s = input('写点吧:')
 f.write(s.encode('utf-8'))
 f.close()

#追加 a ,写在后面,不清除,接着写,不能读,f.read()就是错的
 while 1:
 f = open('d:\python1\\test4.txt',mode='a',encoding='utf-8')
 f.write('9999999')
 f.close()

#追加 a+ ,又可读又可写
 f = open('d:\python1\\test4.txt',mode='a+',encoding='utf-8')
 # s = input('写点吧:')
 # f.write(s)
 # count = f.tell() # 光标位置再哪里
 # f.seek(count-9) # 调节光标位置,-9 就是3个中文字符
 f.seek(0+2)
 print(f.read())
 f.close()



追加 ab 以byts类型 。
 while 1:
     f = open('d:\python1\\test3.txt',mode='ab')
     s = input('写点吧:')
     f.write(s.encode('utf-8'))
     f.close()


 w+ 先写后读 ,只要有w先清除再写,不常用。
 while 1:
 f = open('d:\python1\\test4.txt',mode='w+',encoding='utf-8')
 # s = input('请写入')
 f.write('888')
 f.seek(0)
 print(f.read())
 f.close()



 又读又写 r+,先读后写,如果没有前面有文字
 f = open('d:\python1\\test4.txt',mode='r+',encoding='utf-8')
 s = input('请写入')
 f.write(s)
 count = f.tell()
 print(count)
 print(f.readable())
 line = f.readline()
 光标如何调整到第二行
 print(f.readline()) # 读一行
 print(f.readlines()) # 读多行,每一行当成列表中的一个元素
 lines = 0
#****************如何定位到某个字符***************
# for i in f.readlines():  # i 就是列表中的元素
#     if lines== 2:
#         print(i[1:2])
#         i[1:2].replace('的','316') # 如何替换这个字????
#     lines+=1
#************************************************
# count1 = f.tell()
# print(count1)
# f.seek(count1+8)  # 3为一个中文字符
#==================截取文件========================
# ll = f.truncate(10)  # 截取一段读出来,从前往后截取
# print(ll)
#==========================================
 for line in f:
     print(line)  # 可以对文件句柄循环,打印文件所有内容
 # print(f.read())
 f.close()

 为了防止忘记关闭文件,用下面的with open,自动关闭close,可以打开多个文件
 with open('d:\python1\\test3.txt',mode='r+',encoding='utf-8') as f1,open('d:\python1\\test3.txt',mode='w+',encoding='utf-8') as f2:
     print(f1.read())




# 又读又写 r+b,先读后写,以byts类型 。如果没有前面有文字, open中用b 就不用encoding='utf-8'
 f = open('d:\python1\\test4.txt',mode='r+b')
 s = input('请写入')
 f.write(s.encode('utf-8'))
 print(f.read())
 f.close()

'''
三次登录,读取文件,存到文件
1、注册,把注册的用户名密码保存到文件,打印 print 成功
2 登录,三次登录,从刚才的文件中读取用户名密码再验证
'''

#1 注册
with open('d:\python1\login11.txt',mode='a+',encoding='utf-8') as login:
    print('请注册')
    user= input('用户名:').lower()
    psw = input('密码:')
    login.write(user)
    login.write('\n'+psw)
    count_login =login.tell()
    login.seek(count_login-9)
    print('恭喜你,注册成功')

    # print(login.read())
    # print(logout.tell())

#***********************************************
 # 这种写法有问题,
 #    user1 =logout.readlines()[0]
 #    psw1 = logout.readlines()[1]
 #    print(psw1)
 #    psw1 = logout.readlines()[1]
 #    # IndexError: list index out of range
#************************************************

#2 登录 ,准确找到文件中的用户名密码
times = 0
list = []
while times < 3:
    print('请登录')
    user_login = input('请输入用户名:')
    psw_login = input('请输入密码:')
    # 必须要在用的时候打开,而不是前面打开好了,
    with open('d:\python1\login10.txt', mode='r', encoding='utf-8') as logout:
        for i in logout.readlines():  #  for i in logout:也可以  # 必须要在用的时候打开,而不是前面打开好了,否则报错ValueError: I/O operation on closed file.
            list.append(i)
        user = list[0]
        print(user)
        psw =  list[1]
        print(psw)
        if user.strip() == user_login and psw.strip() == psw_login:  # strip()默认去换行符
            print('登录成功')
        else:
            print('用户名或者密码错误,请重新登录。')
times+=1
print('三次机会已用完')





# 用户登录三次机会

i=input("请输入密码")
psw=123456
times=1
time=3
while times<3:
    if int(i)!=psw:
        print("密码错误,请重新输入")
        time-=1
        print("还有",time,"机会")
        i = input("请输入密码")
    else:
        print("登录成功")
    times+=1
print("三次机会已用完")


 

 

 

posted @ 2020-02-03 11:53  菩提老祖师傅  阅读(3)  评论(0)    收藏  举报