今日内容详细
time模块
时间的三种格式
1.时间戳:从1970年1月开始到现在的秒数,他是一个数字
2.结构化时间:不是给我们看的,让计算机看的,他是时间之间相互转换的时候的中间桥梁
3.格式化时间:就是符合我们人类的习惯的时间格式
2023-09-21 15:05:04
time.time()
time.sleep()
%y 两位数的年份表示(00-99)
%Y 四位数的年份表示(000-9999)
%m 月份(01-12)
%d 月内中的某一天(0-31)
%H 24小时制小时数(0-23)
%I 12小时制小时数(01-12)
%M 分钟数(00-59)
%S 秒(00-59)
%a 本地简化星期名称
%A 本地完整星期名称
%b 本地简化的月份名称
%B 本地完整的月份名称
%c 本地相应的日期表示和时间表示
%j 年内的一天(001-366)
%p 本地A,M.或P,M.的等价符
%U 一年中的星期数(00-53)星期天为星期的开始
%w 星期(0-6),星期天为星期的开始
%W 一年中的星期数(0-53)星期一为星期的开始
%x 本地相应的日期表示
%X 本地相应的时间表示
%Z 当前时区的名称
%% %号本身
print(time.strftime("%Y-%m-%d %X"))
print(time.strftime('%Y-%m-%d %H:%M:%S'))
print(time.strftime('%Y-%m-%d %H-%M-%S'))
res=time.localtime()
print(res)
print(res.tm_year)
print(res.tm_mon)
print(res[0])
print(res[1])
res=time.gmtime(160000000)
print(res)
res=time.localtime(170000000)
print(res)
res=time.localtime(1500000)
res1=time.mktime(res)
print(res1)
res=time.strftime('%Y-%m-%d %X')
res1=time.strftime('%Y-%m-%d',time.localtime(150000000))
print(res1)
res=time.strptime('2017-9-18','%Y-%m-%d')
print(res)
![image-20230921155806432]()
datetime模块
import datetime
res=datetime.datetime.today()
res1=datetime.datetime.now()
print(res,res1)
res=datetime.date.today()
print(res)
res=datetime.date.today()
print(res.year)
print(res.month)
print(res.day)
print(res.weekday())
time.timedelta可以对时间进行操作
delta=datetime.timedelta(days=7)
print(delta)
today=datetime.datetime.today()
delta=datetime.timedelta(days=7,hours=10)
print(today+delta)
print(datetime.datetime.utcnow())
random模块
生成随机数模块,也是内置的模块,直接使用
import random
print(random.random())
print(random.uniform(1,3))
print(random.randint(1,10))
print(random.randrange(1,10))
print(random.randrange(1,10,2))
print(random.choice([1,2,3,[1,2,3,4]]))
print(random.choice(['一等奖','二等奖','三等奖']))
print(random.sample([1,2,[20,25],10,90],2))
item=[1,2,3,4,5,6,678,0]
random.shuffle(item)
print(item)
小练习:
"""随机验证码"""
def get_num(n=4):
sum=''
for i in range(n):
random_int = str(random.randint(1, 9))
random_upper = chr(random.randint(97, 122))
random_lower = chr(random.randint(65, 90))
tmp = random.choice([random_int,random_lower,random_upper])
sum+=tmp
return sum
print(get_num(8))
def get_num(n=4):
code='SF'
sum=''
for i in range(n):
random_int = str(random.randint(100, 999))
date1=time.strftime('%Y%m%d%H%M')
date1+=random_int
code+=date1
return code
print(get_num(20))
OS模块
OS模块是与操作系统交互的一个接口
import os
os.mkdir(r'F:\test')
os.makedirs('myfile/mysecond/mythird')
os.rmdir('myfile/mysecond/mythird')
os.removedirs(r'F:\Python 练习\python20\test\myfile/mysecond')
print(os.listdir(r'F:\Python 练习\python20\test'))
os.remove('a.txt')
os.rename('a.txt','data.txt')
print(os.stat('data.txt'))
print(os.stat(r'F:\Python 练习\python20\test'))
os.system('dir')
res=os.popen('dir').read()
print(res)
res=os.getcwd()
print(res)
res=os.path.abspath(r'F:\Python 练习\python20\test')
print(res)
res=os.path.abspath(r'aaa')
print(res)
res=os.path.dirname(r'F:\Python 练习\python20\test')
print(res)
res=os.path.dirname(os.path.dirname(os.path.dirname(r'F:\Python 练习\python20\test')))
print(res)
res=os.path.basename(r'F:\Python 练习\python20\test/data.txt')
print(res)
res=os.path.exists(r'F:\Python 练习\python20\test/data.txt')
print(res)
res=os.path.exists(r'F:\Python 练习\python20\test')
print(res)
res=os.path.isabs(r'F:\Python 练习\python20\test/data.txt')
print(res)
res=os.path.isfile(r'F:\Python 练习\python20\test\data.txt')
res1=os.path.isfile(r'F:\Python 练习\python20\test\d1.txt')
print(res)
print(res1)
res=os.path.join('bbb','aaa')
print(res)
res=os.path.getatime(r'F:\Python 练习\python20\test\data.txt')
print(res)
res=os.path.getsize(r'F:\Python 练习\python20\test\data.txt')
print(res)
__file__
小视频练习:
basedir = os.path.dirname((os.path.abspath(__file__)))
res = os.path.join(basedir, '小视频')
res1_list = os.listdir(r'%s' % res)
while True:
for i, j in enumerate(range(len(res1_list))):
print(i + 1, res1_list[j])
choice = input(':').strip()
if choice.isdigit():
choice = int(choice)
if choice in range(1, len(res1_list) + 1):
vedio_path=os.path.join(res,res1_list[choice-1])
print(vedio_path)
sys模块
sys模块是与python解释器交互的一个接口
sys.argv
sys.exit(n)
sys.version
sys.path
sys.platform 返回操作系统平台名称
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)