Homework Today:
First, realize the user registration function
Ideas:
User input user name, password
Store user input in a fixed format, such as: egon:123, depositing files
A new user name and password can be re-registered in a previous file
whileTRUE:NAME_INP= Input ('Name>>:'). Strip ()#to verify the user name, create the file beforehand.PWD_INP = input ('Password>>:') Chk_pwd_inp= Input ('Password>>:') ifPWD_INP = =Chk_pwd_inp:with Open ('Db_file', mode=' at', encoding='Utf-8') as F:f.write ('%s:%s\n'%(NAME_INP,PWD_INP))Else: Print('two times password inconsistency')View Code
Second, the realization of user authentication function more:
Ideas:
The user enters the account password, reads the account password from the file, compared with the user input
NAME_INP = input ('Username>>:'). Strip () Pwd_inp= Input ('Password>>:') with open ('Db_file', mode='RT', encoding='Utf-8') as F: forLineinchF:line= Line.strip ('\ n') Line= Line.split (':') name=Line[0] pwd= Line[1] ifNAME_INP = = Name andPWD_INP = =pwd:Print('Validation Successful') Break Else: Print('incorrect user name or password')View Code
Third, write the program, realize the file copy function
Ideas:
User input source file path and destination file path, execute file copy
import sys if len (sys.argv)! = 3: print (" USAGE:CP source_file Target_file " ) Sys.exit () Source_file, Target_file =sys.argv[1],sys.argv[2]with open (source_file, " rb " ) as Read_f, Open (Target_file, " WB " ) as Write_f: for line in Read_f:write_f.write (line)
View Code
Tomorrow morning Dictation:
Looping through files
F=open ('Db_file','RT', encoding='Utf-8') forLineinchF:Print(line) F.close () with open ('Db_file', mode='RT', encoding='Utf-8') as F: forLineinchF:Print(line,end="')View Code
Read-only reading of files and notes
With open ('db_file', mode='rt', encoding=' Utf-8') as F: print(F.read ())1, read only, cannot write 2, when the file does not exist, will error , the file pointer is moved to the beginning of the file when the file exists, and it is at the end of the reading.
View Code
Write only the way the file is written, and note the precautions
With open ('w', mode='wt', encoding='utf-8 ' As F: f.write ('w')1, can only write, cannot read 2, An empty file is created when the file does not exist, and the contents of the file are emptied when the file exists
View Code
Write the file only in the written manner and note the precautions
With open ('w', mode='at', encoding='utf-8 ' As F: f.write ('\nr')1, can only write, cannot read 2, An empty file is created when the file does not exist, and the pointer is moved to the end of the file when the file exists
View Code
A brief description of the difference between T and B modes
T mode: Only text files can be used in T mode, and only text files have the concept of character encoding B mode: must not specify the character encoding, only t mode with the character encoding B is a binary mode, is a common file read mode, because all the files in the hard disk are stored in binary form
View Code
Python-code-05