**time **
import time
res = time.time() #时间戳,从1970年1月1日 00:00:00到当前时间的秒数
print(res) #1612145065.0112846
res = time.localtime() # 本地时区的结构化时间
print(res) #time.struct_time(tm_year=2021, tm_mon=2, tm_mday=1, tm_hour=10, tm_min=8, tm_sec=24, tm_wday=0, tm_yday=32, tm_isdst=0)
# tm_year 年
# tm_mon 月
# tm_mday 日
# tm_hour 时
# tm_min 分
# tm_sec 秒
# tm_wday 一年中的第几周
# tm_yday 一年中的第几天
# tm_isdst 夏令时
print(res.tm_year) #获取结构化时间中的年
%a Locale’s abbreviated weekday name.
%A Locale’s full weekday name.
%b Locale’s abbreviated month name.
%B Locale’s full month name.
%c Locale’s appropriate date and time representation.
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%I Hour (12-hour clock) as a decimal number [01,12].
%j Day of the year as a decimal number [001,366].
%m Month as a decimal number [01,12].
%M Minute as a decimal number [00,59].
%p Locale’s equivalent of either AM or PM. (1)
%S Second as a decimal number [00,61]. (2)
%U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. (3)
%w Weekday as a decimal number [0(Sunday),6].
%W Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. (3)
%x Locale’s appropriate date representation.
%X Locale’s appropriate time representation.
%y Year without century as a decimal number [00,99].
%Y Year with century as a decimal number.
%z Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59].
%Z Time zone name (no characters if no time zone exists).
%% A literal '%' character.
格式化字符串的时间格式
import time
res = time.localtime()
res = time.localtime(1612145706.4231284) #将时间戳转换成当前时区的机构化时间
print(res)
# time.struct_time(tm_year=2021, tm_mon=2, tm_mday=1, tm_hour=10, tm_min=15, tm_sec=6, tm_wday=0, tm_yday=32, tm_isdst=0)
#将结构化时间转成时间字符串的格式,如果右边参数不指定,则获取当前时间的结构化时间作为参数
res = time.strftime('%Y-%m-%d %H:%M:%S')
res = time.strftime('%Y-%m-%d %X')
res = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
print(res) # 2021-02-01 10:31:38
#将格式化时间字符串转换成结构化时间
res = time.strptime('2021-02-01 10:33:21', '%Y-%m-%d %H:%M:%S')
print(res)
# time.struct_time(tm_year=2021, tm_mon=2, tm_mday=1, tm_hour=10, tm_min=33, tm_sec=21, tm_wday=0, tm_yday=32, tm_isdst=-1)
# 获取 Mon Feb 1 10:38:05 2021 格式的时间
# 如果不指定参数,则将当前时间的结构化时间作为参数传入
res = time.asctime()
res = time.asctime(time.localtime())
print(res)
# 把时间戳转换成 Mon Feb 1 10:42:23 2021 的格式
# 如果未指定参数 则会获取当前时间 time.time()作为参数
res = time.ctime()
res = time.ctime(1612147343.8842313)
print(res)
datetime
datetime 用来做时间的加减
import datetime
# 获取当前时间的格式化字符串
res = datetime.datetime.now()
print(res) #2021-02-01 10:46:08.599783
# 当前时间戳转换成日期格式
res = datetime.date.fromtimestamp(time.time())
print(res) #2021-02-01
# 3 days, 0:00:00
print(datetime.timedelta(3))
# 获取三天后的当前时间
res = datetime.datetime.now() + datetime.timedelta(3)
print(res) #2021-02-04 10:52:06.226049
# 获取三天前的当前时间
res = datetime.datetime.now() + datetime.timedelta(-3)
print(res) #2021-01-29 10:52:06.226049
# 当前时间加3小时
res = datetime.datetime.now() + datetime.timedelta(hours=3)
print(res) #2021-02-01 13:56:53.187077
# 当前时间加30分钟
res = datetime.datetime.now() + datetime.timedelta(minutes=30)
print(res) #2021-02-01 11:27:22.140061
# 时间替换
c_time = datetime.datetime.now()
print(c_time) # 2021-02-01 11:00:07.058696
print(c_time.replace(minute=30, hour=2)) # 2021-02-01 02:30:07.058696
random
import random
# 随机出 0-1 之间的小数,大于0且小于1的小数
# <class 'float'>
res = random.random()
print(res) # 0.0625617793262252
# 随机取出大于1 且小于4 的小数
res = random.uniform(1, 4)
print(res) # 3.88391585566657
# 随机出大于等于1 且小于等于4之间的整数
res = random.randint(1, 4)
print(res)
# # 随机出大于等于1 且小于4之间的整数
res = random.randrange(1, 4)
print(res)
# 从列表中随机抽取一个元素
res = random.choice([1, '22', '33', [1, 3], (1, 4)])
print(res) # (1, 4)
# 从列表随机抽取三个元素,右边的参数指定取几个元素
res = random.sample([1, '22', '33', [1, 3], (1, 4)], 3)
print(res) # ['33', '22', [1, 3]]
# 打乱item的顺序
item = [1, 2, 3, 4, 5, 6]
random.shuffle(item)
print(item) # [6, 1, 2, 4, 3, 5]
os 模块
import os
# 获取某个文件夹下的子文件夹和子文件的名字
res=os.listdir('.')
print(res)
#获取文件大小
size=os.path.getsize(r'c.txt')
print(size)
#删除文件
os.remove('bb.jpg')
#重命名文件 (oldname,newname)
os.rename('aa.jpg', 'cc.jpg')
#再python使用linux命令
os.system('ls aaa')
os.environ['aaaaaaaaaa']='111'
print(os.environ)
#去掉文件名,返回目录
print(os.path.dirname('E:\a\b\c.text'))
#获取文件名
print(os.path.basename('E:\a\b\c.text'))
#是否是文件夹
print(os.path.isdir('aaa'))
#是否是文件
print(os.path.isfile('c.txt'))
#拼接路径
print(os.path.join('a','b','c.txt')) #a\b\c.txt