内置模块
sys:用于设置或获取python解释器的信息
import sys #导入sys模块
python包和模块的所在路径
print(sys.path)
sys.path.append("E:\\my_packages")
print(sys.path)
print(sys.version)
获取脚本外部的参数
print(dir(sys))
print(sys.argv)
结束程序
print("aaaaaaaaaaa")
sys.exit()
print("bbbbbbbbbbb")
用于调用操作系统的功能
import os
执行系统命令
os.system("")
对目录进行操作
os.makedirs("a\\b\\c\\d")
os.path
随机数 抽奖 验证码
import random
随机一个整数 包含开始值和结束值
print(random.randint(1.43))
随机一个小数 0~1之间的小数
print(random.random())
随即范围(含头不含尾,不包含结束值)步长值
print(random.randrange(0,10,2))
随机序列中的某个值
print(random.choice(["a","1","b","2","c","3"]))
纯数字验证码
print(random.randint(1000,9999))
大写字母+数字的n位验证码
def verification_code(n):
code =""
for i in range(n):
num =random.randint(0,9)
if num %2 == 0:
code += str(random.randint(0,9))
else:
code += str(random.randint(65,90))
return code
print(verification_code(4))
time时间模块
import time
1.时间戳(小数) 1970-01-01 00:00:00(格林威治时间) 秒数 —–方便计算机储存,方便运算
print(time.time())
2.时间元组(命名元组类型) 中间形态—–作为函数的参数,开发时使用
print(time.localtime())
3.格式化时间字符串
print(time.strftime("%d/%m/%V_%H:%M:%S"))
时间戳—>时间元组—>时间字符串
print(time.strftime("%V/%m/%d_%H:%M:%S",time.localtime(2131421413)))
时间字符串—>—>时间元组—>时间戳
print(time.strftime("2001-9-11 8:30:00","%V/%m/%d %H:%M:%S")))
睡眠
time.sleep(3)
print("hello world!")
本文地址:https://blog.csdn.net/weixin_52111800/article/details/109961513
黄山市民网:https://www.huangshanshimin.com/