文件操作小练习1

1、编写文件copy工具

 with open('ggg.txt', mode='rt', encoding='utf-8') as f1, \
         open('ttt.txt', mode='wt', encoding='utf-8') as f2:
     res = f1.read()
     print(f2.write(res))

 src_file = input('源文件路径>>:').strip()
 dst_file = input('源文件路径>>:').strip()
 with open(r'{
    
    }'.format(src_file),mode='rt',encoding='utf-8') as f1,\
     open(r'{
    
    }'.format(dst_file),mode='wt',encoding='utf-8') as f2:
     res = f1.read()
     f2.write(res)

2、编写登录程序,账号密码来自于文件

 inp_name = input('your name>>:').strip()
 inp_password = input('your password>>:').strip()
 with open('user1.txt', mode='rt', encoding='utf-8') as f:
     for line in f:
         print(line, end='')
         username, password = line.strip().split(':')
         if username == inp_name and password == inp_password:
             print('登陆成功')
             break
     else:
         print('账号或密码失败')

3、编写注册程序,账号密码来存入文件

 name = input('your name>>:').strip()
 password = input('your password>>:').strip()
 with open('user2.txt', mode='at', encoding='utf-8') as f:
     f.write('{
    
    }:{
    
    }\n'.format(name, password))

猜你喜欢

转载自blog.csdn.net/weixin_47237915/article/details/114600969
今日推荐