Python hashlib, OS, Random, sys modules

1 # print(sys.version) #python version
2 # print(sys.path)
3 # print(sys.platform) #What is the current system
4 # print(sys.argv)   #current directory

 

1, hashlib, OS, Random, sys, zipfile module learning, practice

1. hashlib module

hashlib module is mainly used for information encryption, among which it includes many algorithms. Today, MD5, MD5

The underlying algorithm is implemented by hash algorithm. I don't know what it is. In a word, it is a very nb algorithm to add the secret.

Don't talk much, just give me an example

1 import hashlib
2 
3 
4 m=hashlib.md5()
5 m.update('Hello Empty'.encode(encoding='utf-8'))
6 print(m.hexdigest())

Operation result:

2, OS module

The os module is mainly used to interact with the system,

 1 # os.mkdir()  #Directory cannot be created recursively
 2 #os.rmdir() Delete a single empty directory
 3 #os.makedirs(r'c:\a\b\c') #Create directory recursively
 4 #os.removedirs(r'c:\a\b\c') #If the directory is empty, delete it and recurse to the next level. If it is also empty, delete it, and so on
 5 print(os.listdir()) #List files in the current directory
 6 # os.remove('') #Delete a file
 7 # os.rename('') #rename
 8 # print(os.sep) #The output operating system specific path separator win is \ \, Linux/
 9 # print(os.pathsep) #Output string for split file path
10 # print(os.linesep)  #Output line terminator of current platform win\r\n linux \n
11 # print(os.environ)  #Get system environment variables
12 # print(os.name)  #Get the platform win: NT Linex: POSIX
13 # os.system('dir') #Run command
14 #print(os.path.exists(r'c:\Python32')) #Whether the path entered exists

3,Random

Random module is mainly used for random number generators of various distributions

 

 1 import random
 2 
 3 print(random.randint(1,10)) #1-10 Both contain
 4 print(random.randrange(1,10)) #Include previous numbers,Excluding the back
 5 print(random.randrange(0,101,2)) #0-100 Even number between
 6 
 7 print(random.choice('hello')) #Get a random character from a sequence
 8 print(random.sample('kongming',2)) #Get 2 random numbers from the sequence
 9 
10 #Random floating number
11 print(random.random())
12 print(random.uniform(0,10))
13 
14 
15 #Shuffle the cards
16 items=[1,2,3,4,5,6]
17 random.shuffle(items) #Disorganize the original order
18 print(items)

 

A random verification code with random module:

 1 import random
 2 captcha=''
 3 for i in range(6):
 4     chank=random.randrange(0,6)
 5     if chank == i:
 6         tem=chr(random.randint(65,90))
 7     else:
 8         tem=random.randint(0,9)
 9     captcha+=str(tem)
10     
11 print(captcha)

4. sys module

This module provides access to some variables used or maintained by the interpreter and functions that interact strongly with the interpreter

1 # print(sys.version) #python version
2 # print(sys.path)
3 # print(sys.platform) #What is the current system
4 # print(sys.argv)   #current directory

5. zipfile module

Using zipfile to compress files

1 import zipfile
2 z = zipfile.ZipFile("day5.zip","w")
3 z.write("2.txt")
4 print("-----")
5 z.write("1.txt")
6 z.close()

Well, that's it today. It's the same sentence

 

Endless learning, come on

 

 

 

 

Tags: Python Linux encoding

Posted on Sat, 14 Mar 2020 11:57:33 -0400 by JamieinNH