summary
hashilib digest encryption
- There are only two kinds of hash digest (encryption) algorithms: md5 and sha-x series
- Characteristics of hash encryption
- The digest generated by hash algorithm is irreversible. (the original text cannot be obtained through encrypted text)
- Only the same data can be summarized by the same algorithm
- No matter how large the original data is, the size (length) of the generated summary is consistent
- Generate hash summary
- 1 create hash object through algorithm, hashlib Algorithm name ()
- Algorithm name ()
import hashlib hash = hashlib.md5() print(hash) # hash = hashlib.sha1()
-
2 add data
- hash object Update (data)
- Note: the data must be binary and the type is bytes
- Supplement: convert binary and string to each other
-
String to binary:
-
Bytes (string, encoding = 'utf-8')
-
character string. The default value of encode() function is utf-8
hash.update(bytes('234',encoding='utf-8')) hash.update('23435'.encode())
-
-
Binary to string
- Str (binary, encoding = 'utf-8')
- Binary decode()
-
Get summary
result = hash.hexdigest() print(result)
# Determine whether the document has been modified hash = hashlib.md5() with open('text.py','rb') as f: hash.update(f.read()) # print(hash.hexdigest())
File operation
- Data persistence
- The data saved in the program is saved in the running memory by default and will be released at the end of the program. If you want the data generated by running the program this time to be usable the next time you run the program, you need to save the data to the hard disk (disk)
- The process of saving data to the hard disk is the process of data persistence.
- The basic unit of data saved by hard disk is file, so to save data to hard disk, you only need to save data in file
- Common file types of data persistence in programs: database files (. db,.sqlite, etc.), plist files, json files, txt files, binary files (pictures, videos, audio, exe executable files)
- File operation - operate file content
-
os module operation, file name deletion, etc
-
Basic process of file operation: open file - > operate file (read / write) - > close file
-
Open file
""" open(file, mode='r', *, encoding=None) - Open the specified file in the specified way and return a file object a.file - Location information of the file to be opened in the computer (file path), string Absolute path: the full path of the file in the computer (usually written from the disk) Relative path:. - Indicates the current directory (the folder where the current code file is located), ./Can be omitted .. - Represents the upper level directory of the current directory b. mode - The file opening method determines the subsequent operation of the file and the type of operation data after opening the file. Group 1:'r','w','a' - Control subsequent operations (read or write) 'r' - read-only 'w' - Write only, The contents of the original file will be emptied 'a' - Write only, The contents of the original file will be retained Group 2:'t','b' - Controls the type of operation data 't' - The data returned by the read operation and the data written by the write operation are strings(str) 'b' - The data returned by the read operation and the data written by the write operation are binary(bytes) The first group must select one, and the second group can not. If it is not selected, the default is t Note: 1.Binary files must be opened with b c. encoding - Encoding method of text file (it is necessary to ensure the encoding method used for writing data, and the corresponding decoding method is required when reading data), Generally used utf-8,But many windows The default text encoding method is gbk Note: only in t When you open a text file, you need to consider encoding assignment """
# =========Absolute path=========== # open(r'/Users/yuting / lecture / python2103/01 language foundation / day14 exception capture and file operation / contract. txt') # =========Relative path========== # open(r '. / contract. txt') # open('Contract. txt ') # open('./res/a.txt') # open('../day14 - exception capture and file operation / contract. txt') # ========r is read-only========= # f = open('.. / contract. TXT','r ') # f.read() # f.write('abc') # io.UnsupportedOperation: not writable # ========a is write only and retains the contents of the original file======= # f = open('.. / contract. TXT','a ') # f.write('abc') # # f.read() # io.UnsupportedOperation: not readable # ========w is write only and will empty the contents of the original file======= # f = open('.. / contract. TXT','w ') # f.write('abc') # f.read() # io.UnsupportedOperation: not readable # =========t. The operation data is a string====== # f = open('.. / contract. TXT','rt ') # content = f.read() # print(type(content)) # <class 'str'> # =========b. The operation data is binary====== # f = open('.. / contract. TXT','rb ') # content = f.read() # print(type(content)) # <class 'bytes'> # f = open('day7 job. MP4 ',' RB ') # f.read() # 2) Close file # File object close() # f.close() # f.read() # ValueError: read of closed file
# 3) Operation # a. Read operation """ File object.read() - Start from the read-write position and read to the end of the file. (the read / write location is at the beginning of the file by default) File object.readline() - Start from the read-write position and read to the end of a line """ f = open('./res/a.txt', encoding='utf-8') print('============1==============') print(f.read()) print('=============2=============') f.seek(0) # Move the read / write location to the beginning of the file print(f.read()) print('=============3=============') f.seek(0) print(f.readline()) print('=============4=============') print(f.readline()) print('=============5=============') print(f.read()) print('=============6=============') f.seek(0) print(f.readlines()) # ['the bright moon in front of the bed, \ n', 'it is suspected that it is frost on the ground. \ n', 'raise your head to look at the bright moon, \ n', 'bow your head and think of your hometown.'] # b. Write operation # File object Write - writes data to the specified file f = open('./res/a.txt', 'w', encoding='utf-8') f.write('abc')
Data persistence
- How to achieve data persistence
-
Use a file to store data that needs to be persisted
-
When the program needs this data, it reads the data from the file
-
If the data changes, you need to write the latest data into the file again
# Write a program and print the number of times the program is executed f = open('count.txt',encoding='utf-8') count = int(f.read()) count += 1 print(count) f = open('count.txt','w',encoding='utf-8') f.write(str(count))
Eval function
result = eval('100') print(result,type(result)) result = eval('10.3') print(result,type(result)) result = eval('[13,34,45]') print(result,type(result)) result = eval('''{ 'name':'kitten', 'age':12 }''') print(result,type(result))