python 17 days
python 17 days
常用模块一、时间模块、random模块、os模块、sys模块、序列化模块
时间模块:
import time
import time print(time.time())
1526989619.7913861 现在已经发生的流逝的时间
1526956073.6129677 时间戳 —— 机器计算
以秒为单位的 浮点型
1970 1 1 0 0 0 —— 伦敦的时间
1970 1 1 8 0 0 —— 北京的时间
格式化时间 —— 字符串 给人看的
print(time.strftime('%H:%M:%S')) print(time.strftime('%y/%m/%d')) print(time.strftime('%Y-%m-%d')) print(time.strftime('%c'))
19:53:52
18/05/22
2018-05-22
Tue May 22 19:53:52 2018
结构化时间:
print(time.localtime())
time.struct_time(tm_year=2018, tm_mon=5, tm_mday=22, tm_hour=19, tm_min=54, tm_sec=27, tm_wday=1, tm_yday=142, tm_isdst=0)
2018, 5, 22, 10, 36, 31, 1, 142, 0)
字符串时间 <-> 结构化时间 <-> 时间戳时间
时间戳 - 结构化之间的转换:
print(time.time()) print(time.localtime(1500000000)) print(time.localtime(1600000000)) print(time.localtime(2000000000)) print(time.localtime(3000000000)) print(time.gmtime(3000000000))
1526990328.8532526 time.struct_time(tm_year=2017, tm_mon=7, tm_mday=14, tm_hour=10, tm_min=40, tm_sec=0, tm_wday=4, tm_yday=195, tm_isdst=0) time.struct_time(tm_year=2020, tm_mon=9, tm_mday=13, tm_hour=20, tm_min=26, tm_sec=40, tm_wday=6, tm_yday=257, tm_isdst=0) time.struct_time(tm_year=2033, tm_mon=5, tm_mday=18, tm_hour=11, tm_min=33, tm_sec=20, tm_wday=2, tm_yday=138, tm_isdst=0) time.struct_time(tm_year=2065, tm_mon=1, tm_mday=24, tm_hour=13, tm_min=20, tm_sec=0, tm_wday=5, tm_yday=24, tm_isdst=0) time.struct_time(tm_year=2065, tm_mon=1, tm_mday=24, tm_hour=5, tm_min=20, tm_sec=0, tm_wday=5, tm_yday=24, tm_isdst=0)
struct_time = time.localtime(3000000000) print(time.mktime(struct_time))
3000000000.0
struct_time = time.localtime(3000000000) print(time.mktime(struct_time)) ret = time.strftime('%y/%m/%d %H:%M:%S',struct_time) print(ret)
3000000000.0 65/01/24 13:20:00
s_t = time.strptime('2028-5-21','%Y-%m-%d') print(s_t) print(time.mktime(s_t))
time.struct_time(tm_year=2028, tm_mon=5, tm_mday=21, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=142, tm_isdst=-1)
1842451200.0
2018-5-22 11:02:50 —— 时间戳1
2018-5-21 12:59:30 —— 时间戳2
float = 时间戳1 - 时间戳2
float -- > 结构化时间 1970 1 1 23:00:00
1970 1 1 23:00:00 - 1970 1 1 0 0 0
import time ftime1 = '2018-5-22 11:02:50' ftime2 = '2018-5-21 12:59:30' time_format = '%Y-%m-%d %H:%M:%S' struct_time1 = time.strptime(ftime1,time_format) # 格式化转结构化 struct_time2 = time.strptime(ftime2,time_format) stamp_time1 = time.mktime(struct_time1) # 结构化转时间戳 stamp_time2 = time.mktime(struct_time2) sub_time = stamp_time1 - stamp_time2 # 时间戳时间相减 sub_struct_time = time.gmtime(sub_time) # 相减之后的时间戳时间 转 结构化时间 # print(sub_struct_time) print(sub_struct_time.tm_year - 1970, sub_struct_time.tm_mon - 1, sub_struct_time.tm_mday - 1, sub_struct_time.tm_hour, sub_struct_time.tm_min, sub_struct_time.tm_sec, )
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=22, tm_min=3, tm_sec=20, tm_wday=3, tm_yday=1, tm_isdst=0)
0 0 0 22 3 20
ef sub_time(ftime1,ftime2,time_format='%Y-%m-%d %H:%M:%S'): stamp_time1 = time.mktime(time.strptime(ftime1, time_format)) # 格式化转结构化转时间戳 stamp_time2 = time.mktime(time.strptime(ftime2, time_format)) sub_struct_time = time.gmtime(stamp_time1 - stamp_time2) # 相减之后的时间戳时间 转 结构化时间 return (sub_struct_time.tm_year - 1970, sub_struct_time.tm_mon - 1, sub_struct_time.tm_mday - 1, sub_struct_time.tm_hour, sub_struct_time.tm_min, sub_struct_time.tm_sec, ) ftime1 = '2018-5-22 11:02:50' ftime2 = '2018-5-21 12:59:30' ret = sub_time(ftime1,ftime2) print(ret)
(0, 0, 0, 22, 3, 20)
random模块:
import random
手机上 网页上 随机验证码
6位/4位数字 —— 验证码
[0-9] choice 6次 正确
[0-9] sample 6个 错误
s = '' for i in range(6): num = str(random.randint(0,9)) s += num print(s)
615813(随机变动)
6位数字+字母 —— 验证码
chr(97) --> 字符
res = '' for i in range(6): num = str(random.randint(0, 9)) al_num = random.randint(97,122) al_num2 = random.randint(65,90) alpha = chr(al_num) alpha2 = chr(al_num2) res += random.choice([num,alpha,alpha2]) print(res)
j2eD6A (随机变数)
OS模块:
import os
import os print(os.getcwd()) #当前执行所在的目录 os.chdir(r"D:\python视频讲解和课堂笔记\day17视频与课堂笔记\day17") print(os.getcwd())
D:\python视频讲解和课堂笔记\day17视频与课堂笔记\day17
D:\python视频讲解和课堂笔记\day17视频与课堂笔记\day17
import os print(os.listdir('D:\python视频讲解和课堂笔记\day17视频与课堂笔记\day17')) print(os.stat(r'D:\python视频讲解和课堂笔记\day17视频与课堂笔记\day17'))
['1.学习方法.py', '2.复习.py', '3.random模块.py', '4.时间模块.py', '5.os模块.py', '6.sys模块.py', '7.序列化模块.py', '__init__.py'] os.stat_result(st_mode=16895, st_ino=281474976761243, st_dev=3228564640, st_nlink=1, st_uid=0, st_gid=0, st_size=4096, st_atime=1526994478, st_mtime=1526994476, st_ctime=1526977509)
os.system('dir') ret = os.popen('ipconfig').read() print(ret)
������ D �еľ��� �¼Ӿ� ������к��� C06F-FCA0 D:\python��Ƶ����Ϳ��ñʼ�\day17��Ƶ����ñʼ�\day17 ��Ŀ¼ 2018/05/22 21:10 <DIR> . 2018/05/22 21:10 <DIR> .. 2018/05/22 09:10 623 1.ѧϰ����.py 2018/05/22 09:27 850 2.��ϰ.py 2018/05/22 20:56 577 3.randomģ��.py 2018/05/22 20:28 2,815 4.ʱ��ģ��.py 2018/05/22 21:10 1,308 5.osģ��.py 2018/05/22 12:48 527 6.sysģ��.py 2018/05/22 11:21 0 7.���л�ģ��.py 2018/05/22 10:23 2 __init__.py 8 ���ļ� 6,702 �ֽ� 2 ��Ŀ¼ 55,954,317,312 �����ֽ� Windows IP 配置 无线局域网适配器 本地连接* 1: 媒体状态 . . . . . . . . . . . . : 媒体已断开连接 连接特定的 DNS 后缀 . . . . . . . : 以太网适配器 以太网: 连接特定的 DNS 后缀 . . . . . . . : 本地链接 IPv6 地址. . . . . . . . : fe80::f554:acb9:71f4:6f01%10 IPv4 地址 . . . . . . . . . . . . : 192.168.12.97 子网掩码 . . . . . . . . . . . . : 255.255.255.0 默认网关. . . . . . . . . . . . . : 192.168.12.1 无线局域网适配器 本地连接* 11: 连接特定的 DNS 后缀 . . . . . . . : 本地链接 IPv6 地址. . . . . . . . : fe80::ad55:e64b:86a1:6f2b%11 IPv4 地址 . . . . . . . . . . . . : 192.168.137.1 子网掩码 . . . . . . . . . . . . : 255.255.255.0 默认网关. . . . . . . . . . . . . : 以太网适配器 蓝牙网络连接: 媒体状态 . . . . . . . . . . . . : 媒体已断开连接 连接特定的 DNS 后缀 . . . . . . . : 无线局域网适配器 WLAN: 媒体状态 . . . . . . . . . . . . : 媒体已断开连接 连接特定的 DNS 后缀 . . . . . . . : DHCP HOST
import os print(os.path.abspath('4.时间模块.py')) print(os.path.split(r'D:\S12\py笔记\day17\4.时间模块.py')) print(os.path.dirname(r'D:\S12\py笔记\day17\4.时间模块.py')) print(os.path.basename(r'D:\S12\py笔记\day17\4.时间模块.py')) print(os.path.join('D:\\S12\\py笔记\\day17','4.时间模块.py'))
D:\python视频讲解和课堂笔记\day17视频与课堂笔记\day17\4.时间模块.py ('D:\\S12\\py笔记\\day17', '4.时间模块.py') D:\S12\py笔记\day17 4.时间模块.py D:\S12\py笔记\day17\4.时间模块.py
print(os.path.isabs(r'D:\S12\py笔记\day17\4.时间模块.py')) print(os.path.isfile(r'D:\S12\py笔记\day17\4.时间模块.py')) print(os.path.isdir(r'D:\S12\py笔记\day17'))
True
False
False