2020python練習六——檔案處理1
@2020.3.13
#1、撰寫檔案copy工具
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_username=input('your name>>: ').strip() inp_password=input('your password>>: ').strip() # 驗證 with open('user.txt',mode='rt',encoding='utf-8') as f: for line in f: username,password=line.strip().split(':') if inp_username == username and inp_password == password: print('login successfull') break else: print('賬號或密碼錯誤')
#3、撰寫注冊程式,賬號密碼來存入檔案
name=input('your name>>: ') pwd=input('your name>>: ') with open('db.txt',mode='at',encoding='utf-8') as f: f.write('{}:{}\n'.format(name,pwd))
PS:
第2、3 題的測驗代碼中的文本檔案保存路徑默認和源程式代碼的檔案夾一致
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/177780.html
標籤:Python
