defDel_blank_line (filename):#clear File blank line """Clear blank line of file:p Aram FileName: file name: Return:true succeeded; False failed""" Try: With open (filename,"r+", encoding="Utf-8") as Infp:lines= Infp.readlines ()#read the contents of the source file and save it in lineswith open (filename,"w+", encoding="Utf-8") as OUTFP: forLiinchlines:ifLi.split ():#determine if a blank lineOutfp.writelines (LI)#overwrite the source file after the action is written back exceptIOError:Print("%s file does not exist or has no Operation permission"%filename)returnFalseElse: returnTrueclear File blank line
defGet_ip_list (filename, repeat=False):"""obtain a valid IP address in the file:p Aram FileName: file name:p Aram Repeat: Remove duplicate line, true remove, false not remove: return: Returns IP address sequence""" ImportReTry: With open (filename,"R", encoding="Utf-8") as File1:line=file1.read () pattern=Re.compile (R"(?:\ B (?: \ D{1,2}|1\D{2}|2[0-4]\D|25[0-5]) \b\.) {3} (?:\ D{1,2}|1\D{2}|2[0-4]\D|25[0-5]) \b") List_ip=Pattern.findall (line)ifLen (list_ip) = =0:returnlist_ipexceptIOError:Print("%s file does not exist or has no Operation permission"%filename)returnFalseElse: ifrepeat = =True:returnSet (LIST_IP)elifrepeat = =False:returnList_ipget a legitimate IP address in a file
defGet_check_code (n = 6): """gets a random n-bit verification code consisting of uppercase and lowercase letters, numbers:p Aram Num: Verify the number of code bits, the default is 6:return: return n-bit verification code""" ImportRandom Check_code=str () code=str () forIinchrange (n): Ret= Random.randint (0, 9) ifret = = 0orret = = 1orRET = = 4orRET = = 7: Code=STR (ret)elifRET = = 2orRET = = 5orRET = = 8: Code= Chr (Random.randint (65, 90)) elifRET = = 3orRET = = 6orRET = = 9: Code= Chr (Random.randint (97, 122)) Check_code= Check_code +CodereturnCheck_code
Get N-bit random verification code
My method of Python