Python:OS math random datetime time calendar hashlib hmac uuid
OS
# 获取操作系统的名字 Win:nt 非win:posix
print(os.name)
# 路径的分隔符win:\ Linux :/
print(os.sep)
# 获取文件的绝对路径
print(os.path.abspath('filename'))
# 判断是否是文件夹
print(os.path.isdir('filename'))
# 判断是否是文件
print(os.path.isfile('filename'))
# 判断文件是否存在
print(os.path.exists('filename'))
# 实现文件名与后缀的分离
filename = 'name.txt.txt'
print(os.path.splitext(filename)) # ('name.txt', '.txt')
# 获取当前工作目录
print(os.getcwd())
# 切换当前脚本的工作目录,相当于shell下的cd命令
print(os.getcwd())
os.chdir('')
print(os.getcwd())
# 文件夹重命名
os.rename('filename', 'Newfilename')
# 删除 空 文件夹
os.rmdir("filename")
# 创建文件夹
os.mkdir("filename")
# 列出指定目录下的所有文件和文件夹 空默认为当前目录
print(os.listdir())
# 获取到环境配置
print(os.environ)
# 获取指定的环境配置
print(os.environ.get('PATH'))Math
import math # 常量 # inf=inf # +inf 正无穷 -inf 负无穷 # nan=nan # 非数字 not a number # e=2.718281828459045 # pi=3.141592653589793 # tau=6.28318530719586 print(math.fabs(-100)) # 绝对值 print(math.factorial(5)) # 求阶乘 print(math.floor(12.98)) # 向下取整 print(math.ceil(12.001)) # 向上取整 print(math.pow(2, 10)) # 2的10次方 print(math.sin(math.pi / 6)) # 以弧度为单位 print(math.cos(math.pi / 3)) print(math.tan(math.pi / 4))
Random
import random
# randint(2,9) 随机生成区间在[2,9]的整数
print(random.randint(2, 9))
# random() 用来生成区间在[0,1)的随机浮点数
print(random.random())
# randrange(2,9)随机生成区间在[2,9)的整数
print(random.randrange(2, 9))
# 在可迭代对象里随机抽取一个数据项
print(random.choice('abcdefg'))
# 在可迭代对象里随机抽取多个对象
print(random.sample('abcdefg', 2))datetime
import datetime as dt # 当前时间 print(dt.datetime.now()) # 创建一个日期 年月日 print(dt.date(2020, 1, 1)) # 创建一个时间 时分秒 print(dt.time(18, 23, 45)) # 计算三天以后的日期时间 print(dt.datetime.now() + dt.timedelta(3))
Time
import time
# 获取1970-01-01 00:00:00 UTC 到现在时间的秒数
print(time.time())
# 按照指定格式输出时间
time.strftime("%Y-%m-%d %H:%M:%S")
# Fri May 29 23:28:28 2020
print(time.asctime())
# 给一个秒数 由1970-01-01 08:00 生成某一时间
print(time.ctime(0))calendar
import calendar """日历模块""" # 打印2020年的日历,默认以周日为起始日期码 print(calendar.calendar(2020)) # 打印2020年5月的日历 print(calendar.month(2020, 5)) # 将monday设置为起始日期码 calendar.setfirstweekday(calendar, MONDAY) # 闰年返回Ture 否则返回False print(calendar.isleap(2004)) # 获取0-2020年之间有多少闰年 print(calendar.leapdays(0, 2020))
hashlib、hmac
import hashlib
import hmac
"""数据加密"""
# hashlib主要支持两个算法:md5和sha加密
# 加密方式:
# 单向加密md5/sha:只有加密过程,不能解密
# 对称加密、非对称加密rsa
# 需要将加密的内容转换为二进制
# 生成一个md5对象
x = hashlib.md5("abc".encode("utf8"))
# 900150983cd24fb0d6963f7d28e17f72
x.update("qwer".encode("utf8"))
# d1a40f7be1956a26b3a0abb8a7b943b8
print(x.hexdigest())
h1 = hashlib.sha1("123456".encode())
print(h1.hexdigest())
# 一个十六进制占4位 此处224位
h2 = hashlib.sha224("123456".encode())
print(h2.hexdigest())
# 一个十六进制占4位 此处256位
h3 = hashlib.sha256("123456".encode())
print(h3.hexdigest())
# 一个十六进制占4位 此处384位
h4 = hashlib.sha384("123456".encode())
print(h4.hexdigest())
# 一个十六进制占4位 此处512位
h5 = hashlib.sha512("123456".encode())
print(h5.hexdigest())
h = hmac.new('h'.encode(), '你好'.encode())
result = h.hexdigest()
print(result)uuid
import uuid # 用来生成一个全局唯一的id # 32个长度,每个字符16种选择 16**32之1的概率 print(uuid.uuid1()) # uuid4使用的最多 print(uuid.uuid4()) print(uuid.uuid3(uuid.NAMESPACE_DNS, "zhangsan")) # uuid3与uuid5是使用传入的字符串根据指定的算法算出来且固定的 print(uuid.uuid5(uuid.NAMESPACE_DNS, "zhangsan"))
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did127037