在python的三种时间表现形式:
1.时间戳:给电脑看的,自1970-01-01 00:00:00到当前时间,计算了多少秒
2.格式化时间(format string):给人看的,返回的是时间的字符串 2002-01-11
分别是:年(%Y)、月(%m)、日(%d)、时(%H)、分(%M)、秒(%S)、一周的第几天、一年中的第几天,夏令时
?
1.获取时间戳计算时间使用
?
• print(time.time())
?
2.获取格式化时间拼接用户时间格式并保存时使用
?
print(time.strftime(‘%Y-%m-%d‘)) 获取年月日
?
获取年月日时分秒
?
print(time.strfime(‘%Y-%m-%d %H:%M:%S‘))
?
3.获取时间对象
?
print(time.localtime())
?
time.struct_time(tm_year=2019, tm_mon=11, tm_mday=16, tm_hour=14, tm_min=56, tm_sec=10, tm_wday=5, tm_yday=320, tm_isdst=0)
?
print(type(time.localtime()))
?
<class ‘time.struct_time‘>
?
time_obj = time.localtime()
?
print(time_obj.tm_year) #输出今年是哪一年
?
print(time_obj.tm_mon) #输出这个月时哪月
?
res = time.localtime()
?
time.sleep(2) #睡眠2秒
?
获取当前时间的格式化时间
?
print(time.strftime(‘%Y-%m-%d %H-%M-%S‘,time.localtime()))
?
2019-11-16 15:09:24 time.struct_time(tm_year=2019, tm_mon=11, tm_mday=16, tm_hour=15, tm_min=9, tm_sec=24, tm_wday=5, tm_yday=320, tm_isdst=0)
?
将时间对象转为格式化时间
?
print(time.strftime(‘%Y-%m-%d %H:%M:%S‘,res))
?
2019-11-16 15:15:18
?
将字符串式的时间转成时间对象
?
res = time.strptime(‘2019-01-01‘,‘%Y-%m-%d‘)
?
print(res)
?
?
import hashlib
def pwd_md5(pwd):
md5_obj = hashlib.md5()
str1 = pwd
md5_obj.update(str1.encode(‘utf-8‘))
#创造盐
sal = ‘我怎么这么好看!‘
#加盐
md5_obj.update(sal.encode(‘utf-8‘))
#得到一个加密后的字符串
res = md5_obj.hexdigest()
return res
?
?
?
?
?
time.struct_time(tm_year=2019, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=1, tm_isdst=-1)获取当前年月日
?
print(datetime.date.tody())
?
• 2019-11-16
?
获取当前年月时分秒
?
print(datetime.datetime.tody())
?
• 2019-11-16 15:30:23.188891
?
time_obj = datetime.datetime.tody()
?
print(type(time_obj))
?
print(time_obj.year)
?
2019
?
print(time_obj.month)
?
11
?
print(time_obj.day)
?
16
?
从索引0开始计算周一
?
UTC
?
print(time_obj.weekday()) #查看今天是周几 0-6
?
5 即周六
?
ISO
?
print(time_obj.isoweekday())#查看今天是周几1-7
?
6
?
UTC时区
?
北京时间
?
print(datetime.datetime.now()) 查看当前时间
?
2019-11-16 15:52:27.321718
?
格林威治时间与北京时间相差8小时
?
print(datetime.datetime.utcnow())
?
2019-11-16 07:54:13.269957
?
日期/时间的计算
?
日期时间 = 日期时间 ‘+‘ or ‘-‘ 时间对象
?
时间对象 = 日期时间 ‘+ or ‘-‘ 日期时间
?
日期时间:
?
current_time = datetime.datetime.now()
?
print(current_time)
?
2019-11-16 16:00:51.265525
?
时间对象
?
获取7天时间
?
time_obj = datetime.datetime(days =7)
?
print(time_obj)
?
7 days, 0:00:00
?
获取当前时间7天后的时间
日期时间 = 日期时间 ‘+‘ or ‘-‘ 时间对象
later_time = current_time +time_obj
?
print(later_obj)
?
2019-11-23 16:08:27.891113
对象时间 = 日期时间 ‘+ or ‘-‘ 日期时间
time_new_obj = later_time - current_time
print(time_new_obj)
7 days, 0:00:00
随机获取1-9中的任意数
res = random.randint(1,9)
print(res)
默认获取0-1之间的任意小数
res2 = random.random()
print(res2)
洗牌
将可迭代中的值进行乱序
list = [‘红桃A‘,‘梅花A‘,‘红桃Q‘,‘方块K‘]
random.shuffle(list)
print(list)
随机获取可迭代对象中的某一个值
res3 = random.choice(list)
print(list)
需求:随机验证码
需求:大小写字母、数字组合而成
组合5位数的随机验证码
获取任意长度的随机验证码
def get_code(n)
code = ‘‘
for line in range(n):
随机获取一个小写字母
res1 = random.randint(97,122)
lower_str = chr(res1)
随机获取一个大写字母
res2 = random。randint(65,90)
upper_str = chr(res2)
随机获取一个数字
number = str(random.randint(0,9))
code_list = [lower_str,upper_str,number]
random_code = random.choice(code_list)
code += random_code
return code
code = get_code(5)
print(code)
print(len(code))
?
需求:获取当前文件根目录
获取当前文件中的上一级目录
# DAY15_PATH = os.path.dirname(__file__)
# print(DAY15_PATH)
#
# # 项目的根目录,路径相关的值都用 “常量”
# BASE_PATH = os.path.dirname(DAY15_PATH)
# print(BASE_PATH)
#
# # 路径的拼接: 拼接文件 “绝对路径”
# TEST_PATH = os.path.join(DAY15_PATH, ‘老男孩老师们的写真集.txt‘)
# print(TEST_PATH)
#
# # 判断“文件/文件夹”是否存在:若文件存在返回True,若不存在返回False
# print(os.path.exists(TEST_PATH)) # True
# print(os.path.exists(DAY15_PATH)) # True
#
# # 判断“文件夹”是否存在
# print(os.path.isdir(TEST_PATH)) # False
# print(os.path.isdir(DAY15_PATH)) # True
#
# # 创建文件夹
# DIR_PATH = os.path.join(DAY15_PATH, ‘老男孩老师们的写真集‘)
# # os.mkdir(DIR_PATH)
?
# 删除文件夹: 只能删除 “空的文件夹”
# os.rmdir(DIR_PATH)
?
# 获取某个文件夹中所有文件的名字
teacher_list = os.listdir(r‘D:\项目路径\python13期\day15\老男孩老师们的写真集‘)
print(teacher_list)
?
# enumerate(可迭代对象) ---> 得到一个对象,对象有一个个的元组(索引, 元素)
res = enumerate(teacher_list)
print(list(res))
?
# 让用户选择文件
while True:
# 1.打印所有老师的作品
for index, name in enumerate(teacher_list):
print(f‘编号: {index} 文件名: {name}‘)
?
choice = input(‘请选择想看的老师作品-->(头条影片: Jason写真) 编号:‘).strip()
?
# 2.限制用户必须输入数字,数字的范围必须在编号内
# 若不是数字,则重新选择
if not choice.isdigit():
print(‘必须输入数字‘)
continue
?
# 若是数字,往下走判断是否在编号范围内
choice = int(choice)
?
# 判断如果不在列表范围内,则重新选择
if choice not in range(len(teacher_list)):
print(‘编号范围错误!‘)
continue
?
file_name = teacher_list[choice]
?
teacher_path = os.path.join(
r‘D:\项目路径\python13期\day15\老男孩老师们的写真集‘, file_name)
?
print(teacher_path)
?
with open(teacher_path, ‘r‘, encoding=‘utf-8‘) as f:
print(f.read())
获取当前的python解释器的环境变量捷径
print(sys.path)
将当前项目添加到环境变量中
BASE_PATH = os.path.dirname(os.path.dirname(__file__))
sys.path.append(BASE_PATH)
获取cmd终端的命令行 python3 py文件 用户名 密码
print(sys.argv) #返回的是列表[‘py文件‘,‘用户名‘,‘密码‘]
print(‘==‘ *100)
cmd_list = sys.argv
执行文件权限认证
if cmd_list[1] == ‘tank‘ and cmd_list[2] == ‘123‘:
print(‘通过验证‘)
print(‘开始执行逻辑代码‘)
else:
print(‘用户名或者密码错误,权限不足‘)
?
是一个加密模块,内置有很多算法
MD5:不可解密的算法(2018年以前)
摘要算法:摘要就是从某个内容中获取的加密字符串,摘要一样,内容就一定一样,保证唯一性
秘闻密码就是一个摘要
import hashlib
?
md5_obj = hashlib.md5()
?
print(type(md5_obj))
?
str1 = ‘123‘
?
update中一定要传入bytes类型数据
?
md5_obj.update(str1.encode(‘utf-8‘))
得到一个加密后的字符串
res = md5_obj.hexdigest()
print(res)
以上加密撞库可能会破解出真实密码
防止撞库问题:加盐
import hashlib
def pwd_md5(pwd):
md5_obj = hashlib.md5()
str1 = pwd
md5_obj.update(str1.encode(‘utf-8‘))
#创造盐
sal = ‘我怎么这么好看!‘
#加盐
md5_obj.update(sal.encode(‘utf-8‘))
#得到一个加密后的字符串
res = md5_obj.hexdigest()
return res
#模拟用户登陆操作
#获取文件中的用户名与密码
with open(‘user.txt‘,‘r‘,encoding=‘utf-8‘) as f:
user_str = f.read()
file_user,file_pwd = user_str.split(‘:‘)
#用户输入用户名与密码
username = input(‘请输入用户名:‘).strip()
password = input(‘请输入密码:‘).strip()
#校验用户名与密码是否一致
if username == file_user and file_pwd == pwd_md5(passwod):
print(‘登陆成功‘)
else:
print(‘登陆失败‘)
原文:https://www.cnblogs.com/cyfdtz/p/11892288.html