Module Sys,os,hashlib,time,random

Source: Internet
Author: User

Built-in module Sys,os

SYS.ARGV           command line argument list, the first element is the program itself path Sys.exit (n)        exits the program, exit normally (0) sys.version        Gets the version information of the Python interpreter sys.maxint the         largest int value sys.maxunicode the     maximum Unicode value Sys.path           returns the search path for the module, Use the value of the PYTHONPATH environment variable when initializing Sys.platform       returns the operating system platform name Sys.stdout.write (' please: ') val = Sys.stdin.readline () [: -1]print Val
OS.GETCWD () Gets the current working directory, that is, the directory path of the current Python script work os.chdir ("DirName") changes the current script working directory, equivalent to the shell Cdos.curdir return the current directory: ('. ') Os.pardir Gets the parent directory string name of the current directory: (' ... ') Os.makedirs (' dirname1/dirname2 ') can generate a multi-level recursive directory Os.removedirs (' dirname1 ') if the directory is empty, then deleted, and recursively to the previous level of the directory, if also empty, then delete, and so on Os.mkdir (' DirName ') to generate a single-level directory, equivalent to the shell mkdir dirnameos.rmdir (' dirname ') delete the single-level empty directory, if the directory is not empty can not be deleted, error; equivalent to the shell rmdir Dirnameos.listdir (' dirname ') lists all files and subdirectories under the specified directory, including hidden files, and prints os.remove () Delete a file Os.rename ("Oldname", "newname") Rename File/directory Os.stat (' Path/filename ') Get File/directory information OS.SEP output operating system-specific path delimiter, win under "\ \", Linux for "/" OS.LINESEP output the current platform using the line terminator, win under "\t\n", Linux "\ N "os.pathsep output string to split file path Os.name output string indicates the current usage platform. Win-> ' NT ';  Linux-> ' POSIX ' Os.system ("Bash command") runs a shell command that directly displays the Os.environ get system environment variable Os.path.abspath (PATH) Return path normalized absolute path Os.path.split (path) splits path into directory and file name two tuples return Os.path.dirname (path) to the directory where path is returned. In fact, the first element of Os.path.split (path) os.path.basename returns the last file name of path. If path ends with a/or \, then a null value is returned. A second element of Os.path.split (PATH) os.path.exists (Path) If path exists, returns true if path does not exist, return Falseos.path.isabs (PATH) If path is an absolute path, return Trueos.path.isfile (PATH) If path is a file that exists, Returns True. Otherwise, return Falseos.path.isdir (path) True if path is a directory that exists.  Otherwise return Falseos.path.join (path1[, path2[, ...])  When multiple paths are combined, the parameters before the first absolute path are ignored Os.path.getatime (path) returns the last access time of the file or directory to which path is pointing os.path.getmtime (path) Returns the last modified time of the file or directory to which path is pointing

  

Built-in module-hashlib encryption

# ######## MD5 ####### #import hashlibhash = Hashlib.md5 () hash.update (' Shanghai ') print hash.hexdigest ()

Although the above encryption algorithm is still very strong, but the time has the flaw, namely: through the collision library can reverse the solution. Therefore, it is necessary to add a custom key to the encryption algorithm to do encryption.

Import hashlib# ######## md5 ####### #hash = hashlib.md5 (' 898oafs09f ') hash.update (' admin ') print hash.hexdigest ()

Not enough to hang? Python also has an HMAC module that internally creates keys and content for us to process and then encrypt

Import Hmach = hmac.new (' Wueiqi ') h.update (' Hellowo ') print h.hexdigest ()

# ######## SHA1 ######## hash = HASHLIB.SHA1 () hash.update (' admin ') print hash.hexdigest () # ######## sha256 ######## hash = hashlib.sha256 () hash.update (' admin ') print hash.hexdigest ()  # ######## sha384 ######## hash = hashlib.sha384 () Hash.update (' admin ') print hash.hexdigest () # ######## sha512 ######## hash = hashlib.sha512 () hash.update (' admin ') print Hash.hexdigest ()

  

Common Module-time

Three ways to express time

timestamp: Seconds recorded after January 1, 1970

GansoIncludes: year, day, week, etc. formatted string: 2015-11-11 11:11
Import Timeprint time.time ()                   #时间戳的形式显示print time.gmtime ()                  #元祖形式显示print time.strftime ('%y-%m-%d%h:%m:%s ') # Time after string formatting print time.strptime (' 2015-11-11 ', '%y-%m-%d ')  #格式化字符串转换为元祖结构化print time.mktime (Time.localtime ())         #结构化时间转换为时间戳时间

  

#时间运算import datetimeprint Datetime.datetime.now ()   #显示日期时间print datetime.datetime.now ()-Datetime.timedelta ( days=5)  #减去5天print datetime.datetime.now ()-Datetime.timedelta (Hours=1)  #减去1小时print Datetime.datetime.now () + Datetime.timedelta (minutes=10) #加10分钟

  

Random number generation 

Random generates a floating-point number between import Randomprint random.random ()         #随机生成一个0-1 print random.randint (1,5)     # Randomly generates an integer between 1-5 and an integer between print random.randrange (1,5)    #随机生成一个1-4

  

Random generates an arbitrary letter import randomtemp = Random.randint (65,90) print Chr (temp)    #chr () function Ascill numeric to character word to character

Random generates 6-bit verification code

Import randomlist = []for i in range (6):  #循环6次, 0-5    if i = = Random.randint (1,5):        list.append (str ( Random.randint (1,5)))        #转换为str方便jion操作list    else:        temp = random.randint (65,90)        list.append (Chr ( temp)) Print  "". Join (list)   #jion () list converted to string

  

Module Sys,os,hashlib,time,random

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.