Common Python modules

Source: Internet
Author: User

Common Python modules

The module is essentially a. py file, which can be seen in the lib folder under the installation directory.

The module is divided into three parts: the built-in module (in the interpreter), the third-party module (in the lib folder), and the custom module (defined by yourself)

1. time Module

Import time # returns the current timestamp print (time. time () #1498027773.1063557 # returns a structured time tuples with the timestamp as the parameter. The default value of this parameter is the current time print (time. localtime (1912412470) # time. struct_time (tm_year = 2030, tm_mon = 8, tm_mday = 8, tm_hour = 17, tm_min = 41, tm_sec = 10, tm_wday = 3, tm_yday = 220, tm_isdst = 0) # returns the structured Greenwich Mean time With the timestamp as the parameter. The default parameter is the current time print (time. gmtime () # time. struct_time (tm_year = 2017, tm_mon = 6, tm_mday = 21, tm_hour = 6, tm_min = 49, tm_sec = 2, tm_wday = 2, tm_yday = 172, tm_isdst = 0) # convert structured time to string time print (time. strftime ('% Y: % m: % d-% x', time. localtime () #14:48:50-# convert string time to the formatting time print (time. strptime ('2017-10-15 ',' % Y-% m-% D') # time. struct_time (tm_year = 1993, tm_mon = 10, tm_mday = 15, tm_hour = 0, tm_min = 0, tm_sec = 0, tm_wday = 4, tm_yday = 288, tm_isdst =-1) # convert structured time to print (time. mktime (time. localtime () #1498027859.0 print (time. asctime () # Wed Jun 21 14:53:11 2017 print (time. ctime () # Wed Jun 21 14:53:11 2017 # The thread delays running at the specified time. The parameter is the specified time in seconds. sleep (5)

Conversion of several time formats:

 

2. random Module

Import randomprint (random. random () # returns a random floating point 0.40165771797504246 print (random. randint () # returns a random value of the int Type 3 print (random. randrange (1, 3) # returns a random int type 1 print (random. choice ([, 'A', 'B']) # returns an element 12 print (random. sample ([, 34, 'A', 'B'], 3) # returns the elements in the specified list of specified numbers randomly ['A', 21, 'B'] print (random. uniform () # returns a random floating point number 2.216248301_20144l = [1, 2, 3, 4, 5, 6] random. shuffle (l) # disrupt the order of the specified list print (l) # [1, 3, 6, 2, 4, 5]

Exercise

# Exercise: randomly generate a five-digit verification code, including numbers and uppercase/lowercase letters import randomres = ''for I in range (5): num = random. randint (0, 9) alpA = chr (random. randint (65,90) alpa = chr (random. randint (97,122) s = random. choice ([str (num), alpA, alpa]) res = res + sprint (res)

3. OS Module

1 import OS 2 3 # obtain the current working directory 4 print (OS. getcwd () # C: \ untitled \ 0621day12 5 6 # change the current working directory, which is equivalent to the cd 7 OS in shell. chdir (file_path) 8 9 # generate multi-layer recursive directory 10 OS. makedirs ('dirname1/dirname2') 11 12 # recursively Delete the empty directory 13 OS. removedirs (path) 14 15 # generate a single-level directory 16 OS. mkdir ('dirname') 17 18 # delete a single-stage empty directory. If the directory is not empty, 19 OS cannot be deleted. rmdir (path) 20 21 # list all files and subdirectories in the specified directory, including hidden files, and print 22 print (OS. listdir (r 'C: \ untitled \ 0612Python 5day') #['2017 job. py ', 'a.txt', 'job topic ', 'character encoding. py ',' file operations. py '] 23 24 # delete a file 25 OS. remove () 26 27 # rename the file/directory 28 OS. rename ('oldname', 'newname') 29 30 # Get File/directory information 31 OS. stat (path) 32 print (OS. stat (r 'C: \ untitled \ 0612Python 5day') # OS. stat_result (st_mode = 16895, st_ino = 2814749767244350, st_dev = 3504670893, st_nlink = 1, st_uid = 0, st_gid = 0, st_size = 4096, st_atime = 1497356489, st_mtime = 1497356489, st_ctime = 1497227714) 33 34 # The returned file directory is actually the OS. path. the first element of split (path) is 35 OS. path. dirname (path) 36 37 # the file name returned is actually OS. path. the second element of split (path) is 38 OS. path. basename (path) # If path ends with // or \, a null value of 39 40 is returned. # If the file exists, True is returned. Otherwise, False41 OS is returned. path. exists (path) 42 43 # If path is an absolute path, True44 OS is returned. path. isabs (path) 45 46 # returns the result after combining multiple paths. The parameters before the first absolute path are ignored for 47 OS. path. join (path, paths) 48 49 # Return the last access time of the file or directory pointed to by path 50 OS. path. getatimme (path) 51 52 # Return the last modification time of the file or directory pointed to by path 53 OS. path. getmtime (path) 54 55 # Return the size of path 56 OS. path. getsize (path)

4. hashlib

The hashlib module provides common digest algorithms, such as MD5 and SHA1. In Python3, hash directly replaces MD5 and SHA.

A function is used to convert data of any length into a fixed data string.

1. The Digest algorithm is irreversible and cannot be used to obtain metadata from data strings.

2. Even if there is only one-bit difference between the two data, the summary is irrelevant.

3. No matter how big the metadata is, the obtained summary is of a fixed length.

4. When the metadata is large, one row needs to be read and one row is verified. The last. hexdigest () can digest all the previous verification results.

MD5 is very fast and often used. The generated result is a fixed-bit string, which is usually expressed by a 32-bit hexadecimal string.

import hashlibmd5=hashlib.md5()md5.update('aasg'.encode('utf-8'))#434c591c9c5a04e44ec8dbafe81058e7md5.update('adasdg'.encode('utf-8'))#d20a55b2194493fdeb66e2cd358dab15md5.update('aasgadasdg'.encode('utf8'))#d20a55b2194493fdeb66e2cd358dab15print(md5.hexdigest())

Not yet to be continued ....

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.