Random Verification Code generation (Python implementation)

Source: Internet
Author: User

Requirement: Generate random non-duplicate verification code.

Code:

#!/usr/bin/env python#Encoding:utf-8"""@author: The great man KAMIL@FILE:200NUMBER.PY@TIME:2016/4/13 23:33"""Importrandom,stringdefRand_str (num,length = 7): F= Open ("Activation_code2.txt","WB")     forIinchrange (num): Chars= String.ascii_letters +string.digits S= [Random.choice (chars) forIinchrange (length)] F.write (bytes ("'. Join (s) +'\ n'),'Utf-8'))#f.write ('. Join (s) + ' \ n ') py2f.close ()if __name__=="__main__": Rand_str (200)

will be written in the file line by row, involving knowledge points: F.open F.writer random

#' str ' does not support the buffer interface in Python3 errorWith open ("Test.txt") as Fp:line=Fp.readline () with open ("Test.out",'WB') as Fp:fp.write (line)#solution 1 needs to encode str in python3x,With open ("Test.txt") as Fp:line=Fp.readline () with open ("Test.out",'WB') as Fp:fp.write (bytes (line,'Utf-8'))#Solution 2 does not want to write in B (binary) mode, so this error can be avoided by using the T (text, which is the default mode for write) mode write.With open ("Test.txt") as Fp:line=Fp.readline () with open ("Test.out",'WT') as FP:#with open ("Test.out", ' W ') as FP:Fp.write (line)

Random Verification Code generation (Python implementation)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.