1.ramdom 模块
# 生成随机数模块,也是一个内置的模块,直接使用
import random
import random
#
# print(random.random()) # 0.607068057472247 # 大于0且小于1之间的小数
#
# print(random.uniform(1, 3)) # 大于1小于3的小数
#
# """生成随机整数"""
# # print(random.randint(1, 10)) # 大于等于1且小于等于5之间的整数
# print(random.randrange(1, 10, 2)) # 大于等于1且小于10之间的奇数
#
# # 随机选择一个返回
# print(random.choice([1, 2, 33, 4, 55, [11, 22]])) # #1或者23或者[4,5]
# # random.choice(1, 2, 3, 4, 5)
# # random.choice([1,2 ,3, 5,])
#
# print(random.choice(['一等奖', '二等奖', '三等奖', '四等奖', '五等奖', ])) # 每次随机出来一个结果
# """抽奖:商家搞活动抽奖、公司年会上抽奖"""
# # 微信小程序:抽奖助手(基本是公平,一亿用户,10个)
#
# print(random.sample([1, '23', [4, 5], 4, 5, 6, 7, 8, 9], 3)) # #列表元素任意2个组合
#
# # 打乱列表顺序
# item = [1, 3, 5, 7, 9]
# random.shuffle(item) # 打乱次序 洗牌
#
# res = ['A', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K']
# random.shuffle(res)
# print(res)
import time
"""随机验证码"""
# 1. 数字、字母(大写、小写的)
# 2. 位数(4位、5位、6位)
# ### 循环生成随机验证码
def get_code(n=4):
code = 'SF'
for i in range(n):
random_int = str(random.randint(0, 9)) # 0-9之间的整数
# random_upper = random.choice(['A', 'B', 'C', 'D', 'E',])
random_upper = chr(random.randint(65, 90))
random_lower = chr(random.randint(97, 122))
temp=random.choice([random_int, random_upper, random_lower])
code += temp # A2E5
return code
### 循环生成随机验证码
def get_out_order():
code = 'SF' + time.strftime('%Y%m%d%H%M%S') # SF20230921103119
for i in range(8):
random_int = str(random.randint(0, 9)) # 0-9之间的整数
# random_upper = random.choice(['A', 'B', 'C', 'D', 'E',])
random_upper = chr(random.randint(65, 90))
random_lower = chr(random.randint(97, 122))
temp=random.choice([random_int, random_upper, random_lower])
code += temp # A2E5
return code
"""生成一个订单号: SF32143432432432"""
"""生成一个订单号: TB32143432432432"""
"""生成一个订单号: YT20230921458946"""
print(get_code())
print(get_code(5))
print(get_code(6))
print(time.time()) # 1695263424.6257682
print(time.strftime('%Y%m%d%H%M%S')) # 1695263424.6257682
print(get_out_order()) # SF20230921103353YUoA SF202309211033593HIF
import time
# 生成订单号
def get_order_code():
# 年月日时分秒+time.time()的后7位
order_no = str(time.strftime('%Y%m%d%H%M%S', time.localtime(time.time())) + str(time.time()).replace('.', '')[-7:])
return order_no
print(get_order_code())
2.os模块
os模块是与操作系统交互的一个接口
import os # operate system
"""一次只能创建一层目录"""
# make directory
# os.mkdir('myfirst') # 生成单级目录;相当于shell中mkdir dirname
# os.mkdir('myfirst') # 生成单级目录;相当于shell中mkdir dirname
# os.makedirs('myfirst/mysecond/mythird') # 可生成多层递归目录
# rm:remove
# os.rmdir('aaa') # 删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname
# os.removedirs(r'D:\Python27\day23\myfirst\mysecond') # 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
# res=os.listdir(r'D:\Python27\day23\aaa') # 列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
# print(res) # ['.qaz', '1.txt', '2.txt', '3.txt', 'abc']
# os.remove(r'D:\Python27\day23\aaa\1.txt') # 删除一个文件
# os.rename(r"D:\Python27\day23\bbb\2.txt", r"D:\Python27\day23\bbb\2222.txt") # 重命名文件/目录
"""修改文件内容"""
# res=os.stat(r'D:\Python27\day23\bbb\3.txt') # 获取文件/目录信息
# print(res)
"""
os.stat_result(st_mode=33206, st_ino=3659174698001585, st_dev=2316620894, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1695264600, st_mtime=1695264600, st_ctime=1695264600)
st_atime
st_mtime
st_ctime
"""
# bash解释器 command dir tasklist print()
# python-----》解释器
# shell脚本-----》python脚本
# index.sh ------> index.py
# index.sh----->/bin/bash----->python.exe
# os.system("tasklist") # 运行shell命令,直接显示 python linux:
# os.system("dir") # 运行shell命令,直接显示 python linux:
# res=os.popen("tasklist").read() # 运行shell命令,获取执行结果
# print(res)
# res=os.getcwd() # 获取当前工作目录,即当前python脚本工作的目录路径
# print(res) # D:\Python27\day23
# ch: change directory
# cd:D:\Python27\day23\bbb
os.chdir(r"D:\Python27\day23\bbb") # 改变当前脚本工作目录;相当于shell下cd
# res = os.getcwd()
# print(res)
os.mkdir('aaa')
# os.path
# res=os.path.abspath(r'D:\Python27\day23\bbb') # 返回path规范化的绝对路径os.path.split(path) 将path分割成目录和文件名二元组返回
# res=os.path.abspath(r'D:\Python27\day23\bbb\3.txt') # 返回path规范化的绝对路径os.path.split(path) 将path分割成目录和文件名二元组返回
# absolute 绝对的
# relative 相对的
# 绝对定位 相对定位
# res=os.path.abspath(r'bbb') # 返回path规范化的绝对路径os.path.split(path) 将path分割成目录和文件名二元组返回
# print(res) # D:\Python27\day23\bbb D:\Python27\day23\bbb\3.txt D:\Python27\day23\bbb
# res=os.path.dirname(r'D:\Python27\day23\bbb\3.txt') # 返回path的目录。其实就是os.path.split(path)的第一个元素
# res=os.path.dirname(os.path.dirname(r'D:\Python27\day23\bbb\3.txt')) # 返回path的目录。其实就是os.path.split(path)的第一个元素
# res=os.path.dirname(r'D:\Python27\day23\bbb') # 返回path的目录。其实就是os.path.split(path)的第一个元素
# res=os.path.dirname(r'D:\Python27\day23') # 返回path的目录。其实就是os.path.split(path)的第一个元素
# res=os.path.dirname(os.path.dirname(os.path.dirname(r'D:\Python27\day23\bbb\3.txt')))
# print(res) # D:\Python27\day23\bbb
#
# res=os.path.basename(r'D:\Python27\day23\bbb\3.txt') # 返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素
# print(res) # 上传图片的时候会用到 D:\Python27\day23\bbb\1.png # 验证后缀名,png jpg jpeg
"""它可以判断文件,也可以判断文件夹"""
# res=os.path.exists(r'D:\Python27\day23\bbb\3.txt') # 如果path存在,返回True;如果path不存在,返回False
# res=os.path.exists(r'D:\Python27\day23\bbb\aaaa') # 如果path存在,返回True;如果path不存在,返回False
# print(res)
# if not os.path.exists(r'D:\Python27\day23\ccc'):
# os.mkdir(r'D:\Python27\day23\ccc')
# res=os.path.isabs(r'D:\Python27\day23\ccc') # 如果path是绝对路径,返回True
# res=os.path.isabs(r'ccc') # 如果path是绝对路径,返回True
# print(res)
# res=os.path.isfile(r'D:\Python27\day23\bbb\3.txt') # 如果path是一个存在的文件,返回True。否则返回False
# res1=os.path.isdir(r'D:\Python27\day23\bbb\3.txt') # 如果path是一个存在的文件,返回True。否则返回False
# print(res)
# print(res1)
# base_dir = r'D:\Python27\day23' # // \ /
# path = base_dir + '\\' + 'bbb' + '\\' + 'aaa' # D:\Python27\day23\bbb\aaa
# print(path)
# """会检测系统,Windows \\ linux // """
# """路径的拼接,不用考虑路径分隔符"""
# # res=os.path.join(base_dir, 'bbb', 'aaa') # 将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
# res=os.path.join(base_dir, 'bbb', 'aaa', 'qqq', 'aaa', 'zzz', 'xxx') # 将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
# print(res)
res=os.path.getatime(r'D:\Python27\day23\bbb\3.txt') # 返回path所指向的文件或者目录的最后访问时间
res1=os.path.getmtime(r'D:\Python27\day23\bbb\3.txt') # 返回path所指向的文件或者目录的最后修改时间
print(res, res1)
# 0字节
res2=os.path.getsize(r'D:\Python27\day23\bbb\3.txt') # 返回path的大小
print(res2)
"""判断文件是否为空"""
3.sys模块
sys模块是与python解释器交互的一个接口
sys.argv 命令行参数List,第一个元素是程序本身路径
sys.exit(n) 退出程序,正常退出时exit(0),错误退出sys.exit(1)
sys.version 获取Python解释程序的版本信息
sys.path 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
sys.platform 返回操作系统平台名称
print(sys.argv)
try:
username = sys.argv[1]
password = sys.argv[2]
except Exception as e:
print('参数异常')
print(username)
print(password)