2020_10_19_Common modules

Commonly used standard library

1. Math module math (general math module), cmath (complex math module)

2. Random module random: randint, random, shuffle, choice, choices

3. Processing file and directory module os:

os.chdir(路径)   # 将指定路径对应的目录修改成当前目录
                 # (默认情况下当前目录是当前py文件所在的目录)

os.getcwd()   # 获取当前目录的全路径

os.remove(path)	  # 用于删除指定路径的文件。如果指定的路径是一个目录,将抛出OSError。在Unix, Windows中有效。

os.unlink(path)     # 用于删除文件,如果文件是一个目录则返回一个错误

os.rename(src, dst)    # 用于命名文件或目录,从 src 到 dst,如果dst是一个存在的目录, 将抛出OSError。	src-要修改的目录名;dst-修改后的目录名。

os.open(file, flags[, mode])    # 用于打开一个文件,并且设置需要的打开选项,模式参数mode参数是可选的,默认为 0777。		file -- 要打开的文件;flags -- 该参数可以是以下选项,多个使用 "|" 隔开:os.O_RDONLY: 以只读的方式打开,os.O_WRONLY: 以只写的方式打开,os.O_RDWR : 以读写的方式打开,os.O_APPEND: 以追加的方式打开,mode -- 类似 chmod()。

os.listdir(path)	# 用于返回指定的文件夹包含的文件或文件夹的名字的列表。这个列表以字母顺序。 它不包括 '.' 和'..' 即使它在文件夹中。只支持在 Unix, Windows 下使用。		返回值-返回指定路径下的文件和文件夹列表。

# ===============================================

# os.path 模块的几种常用方法:
os.path.abspath(path)	# 返回绝对路径
os.path.basename(path)	# 返回文件名
os.path.commonprefix(list)	# 返回list(多个路径)中,所有path共有的最长的路径
os.path.dirname(path)	# 返回文件路径
os.path.exists(path)	# 路径存在则返回True,路径损坏返回False
os.path.lexists	  # 路径存在则返回True,路径损坏也返回True
os.path.expanduser(path)	# 把path中包含的"~"和"~user"转换成用户目录
os.path.expandvars(path)	# 根据环境变量的值替换path中包含的"$name"和"${name}"
os.path.getatime(path)	# 返回最近访问时间(浮点型秒数)
os.path.getmtime(path)	# 返回最近文件修改时间
os.path.getctime(path)	# 返回文件 path 创建时间
os.path.getsize(path)	# 返回文件大小,如果文件不存在就返回错误
os.path.isabs(path)	  # 判断是否为绝对路径
os.path.isfile(path)	# 判断路径是否为文件
os.path.isdir(path)	  # 判断路径是否为目录
os.path.islink(path)	# 判断路径是否为链接
os.path.join(path1[, path2[, ...]])	  # 把目录和文件名合成一个路径
os.path.normcase(path)	# 转换path的大小写和斜杠
os.path.normpath(path)	# 规范path字符串形式
os.path.realpath(path)	# 返回path的真实路径
os.path.relpath(path[, start])	# 从start开始计算相对路径
os.path.samefile(path1, path2)	# 判断目录或文件是否相同
os.path.sameopenfile(fp1, fp2)	# 判断fp1和fp2是否指向同一文件
os.path.samestat(stat1, stat2)	# 判断stat tuple stat1和stat2是否指向同一个文件
os.path.split(path)	 # 把路径分割成 dirname目录名 和 basename文件名,返回一个元组
os.path.splitdrive(path)	# 一般用在 windows 下,返回驱动器名和路径组成的元组
os.path.splitext(path)	# 分割路径中的文件名与拓展名

4.json

5. Regular expression related operation module re

6. sys module (no need to import): exit() ends the thread early

7. Drawing module turtle: setup

8.csv file operation module csv

9. Mail sending module smtplib

10. Time related modules time and dateTime

The timestamp is the time difference expressed in seconds to 0:00:0 on January 1, 1970, Greenwich Mean Time.
The benefits of timestamp: saving time information saves memory more than saving time information directly; it is more convenient to encrypt timestamp than string time encryption.

grammar:

time.time()  # 获取当前时间戳
time.localtime()  # 获取当前的本地时间,返回struct_time对象
time.localtime(时间戳)  # 获取指定时间戳对应的本地时间,返回struct_time对象
strftime(时间格式字符串, 结构体时间) # 将结构体时间转换成指定格式的字符串时间
strptime(字符串, 时间格式字符串) # 将字符串时间转换成结构体时间

11.hashlib

The hashlib module mainly provides algorithms related to hash encryption to encrypt data

Features: The encryption result is irreversible (the original text cannot be obtained through the ciphertext/summary); the result after the same data is encrypted by the same algorithm is the same; different data has the same length after being encrypted by the same algorithm

Application scenario: password preservation; data integrity verification

Encryption steps:

# 1.根据算法创建hash对象
hash1 = hashlib.md5()

# 2.添加需要生成摘要/密文的数据
# hash对象.update(数据的二进制)
hash1.update('123456'.encode(encoding='utf-8'))

# 生成摘要
dig = hash1.hexdigest()
print(dig)

Guess you like

Origin blog.csdn.net/xdhmanan/article/details/109172418