1.time 和 datetime 模块
import time
# -----time-----
# 1.时间三种格式
# 1.1 时间戳
data = time.time()
print(data,type(data))
# 1596107844.092223 <class 'float'>
# 1.2 格式化时间
res1 = time.strftime('%Y-%m-%d %H:%M:%S %p') ### %p是说上午下午
res2 = time.strftime('%Y-%m-%d %X') ### %X是简写时间
print(res1,res2,type(res2))
# 2020-07-30 19:17:24 PM 2020-07-30 19:17:24 <class 'str'>
# 1.3 结构化时间-获取时间的一部分
res1 = time.localtime()
res2 = time.gmtime() # UTC时间 格林尼治时间
print(res1,type(res1))
# time.struct_time(tm_year=2020, tm_mon=7, tm_mday=30, tm_hour=19, tm_min=17, tm_sec=24, tm_wday=3, tm_yday=212, tm_isdst=0) <class 'time.struct_time'>
print(res1.tm_year)
# 2020
print(res2)
# time.struct_time(tm_year=2020, tm_mon=7, tm_mday=30, tm_hour=11, tm_min=17, tm_sec=24, tm_wday=3, tm_yday=212, tm_isdst=0)
# 2. 时间转换
# 2.1 时间戳-格式化字符串
struct_time =time.localtime(333.33)
res = time.strftime('%Y-%m',struct_time)
print(res)
# 1970-01
# 2.2 格式化字符串-时间戳
struct_time = time.strptime('2020-10-20 11:11:11','%Y-%m-%d %H:%M:%S')
res = time.mktime(struct_time)
print(res)
# 1603163471.0
# 3.
print(time.ctime(3333.3)) # 接受一个时间戳,可以不传
# Thu Jan 1 08:55:33 1970
# Thu Jan 1 08:55:33 1970 此格式为在Linux系统中显示方式
print(time.asctime(time.localtime(3333.3))) # asctime接受一个结构化时间,可以不填
# Thu Jan 1 08:55:33 1970
# 4.time.sleep
time.sleep(3)
# -----datetime-----
from datetime import datetime,timedelta
res = datetime.now() # 默认就是格式化时间
print(res)
# 2020-07-30 19:17:27.117053
print(datetime.fromtimestamp(time.time())) # 接受一个时间戳 变回格式化时间
# 2020-07-30 19:17:27.117053
res1 = datetime.now() + timedelta(days=3)
res2 = datetime.now() - timedelta(weeks=4)
print(res1,res2)
# 2020-08-02 19:17:27.118055 2020-07-02 19:17:27.118055
res = datetime.now()
print(res)
# 2020-07-30 19:17:27.118055
new_res = res.replace(year=1999,month=9,day=9)
print(new_res)
# 1999-09-09 19:17:27.118055
2.random 模块
import random
print(random.random()) # 默认 0-1 之间的小数
# 0.9394421790710175
print(random.randint(1,3)) # 包含 整数
# 1
print(random.choice([1,2,3,4,[5,6]])) # 随机选择一个元素
# [5, 6]
print(random.sample([1,2,3,4,[5,6]],3)) # 随机选择第二个参数个元素
# [4, 3, [5, 6]]
print(random.uniform(1,3)) # 不包含 小数
# 2.855080541726659
item=[1,2,3,4,5]
random.shuffle(item) # 打乱
print(item)
# [5, 3, 1, 2, 4]
# 随机验证码
def code(n=6):
res=''
for i in range(n):
s1 = str(random.randint(0,9))
s2 = chr(random.randint(65,90))
s3 = chr(random.randint(97,122))
info = random.choice([s1,s2,s3])
res += info
return res
data=code()
print(data)
# o4K1H4
3.os 模块
import os
print(os.listdir('.')) # 当前文件夹下的文件
# ['.idea', '01 time和datetime模块.py', '02 random模块.py', '03 os模块.py', '04 subprocess模块.py', '05 sys模块.py', '06 打印进度条.py', 'day21.md']
print(__file__) # 当前文件路径
# C:/Users/Usher/Desktop/全栈15期/day21/03 os模块.py
print(os.path.abspath(__file__)) # 当前文件的绝对路径
# C:UsersUsherDesktop全栈15期day21 3 os模块.py
print(os.path.split(os.path.abspath(__file__))) # 拆分当前文件夹和当前文件
# ('C:\Users\Usher\Desktop\全栈15期\day21', '03 os模块.py')
print(os.path.dirname(os.path.abspath(__file__))) # 当前文件夹
#C:UsersUsherDesktop全栈15期day21
print(os.path.basename(os.path.abspath(__file__))) # 当前文件
# 03 os模块.py
print(os.path.exists(r'C:UsersUsherDesktop全栈15期day21 3 os.py')) # 此路径是否存在
# False
print(os.path.isabs(r'C:UsersUsherDesktop全栈15期day21 3 os.py')) # 此路径是否是一个绝对路径
# True
# 关于求上级目录
info = os.path.join(__file__,'..')
info_path = os.path.normpath(info)
print(info_path)
# C:UsersUsherDesktop全栈15期day21
#或者
print(os.path.dirname(__file__))
# C:/Users/Usher/Desktop/全栈15期/day21
print(os.path.join('a','b','c'))
# ac
# 手动做一个CMD
cmd = input ('请输入内容')
res = os.system(cmd)
print('*'*10)
print(res) # 此处在真正的CMD中运行显示正常 但是在编译器中则会乱码 因为默认编码不同,可以用subprocess解决
4.subprocess 模块
import subprocess
# 用到了管道这个词 PIPE
obj = subprocess.Popen("dir", shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
) # 'dir'这个位置的任务随意设置
res1=obj.stdout.read()
res2=obj.stderr.read()
print(res1.decode('gbk'))
print(res2.decode('gbk'))
'''
驱动器 C 中的卷没有标签。
卷的序列号是 74EE-16DC
C:UsersUsherDesktop全栈15期day21 的目录
2020/07/30 19:23 <DIR> .
2020/07/30 09:18 <DIR> ..
2020/07/30 19:23 <DIR> .idea
2020/07/30 19:19 2,138 01 time和datetime模块.py
2020/07/30 19:20 788 02 random模块.py
2020/07/30 19:23 1,593 03 os模块.py
2020/07/30 18:31 367 04 subprocess模块.py
2020/07/30 19:06 339 05 sys模块.py
2020/07/30 19:15 445 06 打印进度条.py
2020/07/30 19:16 11 day21.md
7 个文件 5,681 字节
3 个目录 9,336,905,728 可用字节
Process finished with exit code 0
'''
5.sys 模块
import sys
# file1 =input('原文件路径').strip()
# file2 = input('目标文件路径').strip()
print(sys.argv)
file1 = sys.argv[1]
file2 = sys.argv[2]
with open (r'%s' % file1 , mode='rb') as f1 , open (r'%s' % file2 , mode='wb') as f2:
for line in f1:
f2.write(line)
# 命令行引号用双引号!!!
# 终端中命令行如下:
# python "C:UsersUsherDesktop全栈15期day21 5 sys模块.py" "C:UsersUsherDesktop全栈15期day21 5 sys模块.py" "C:UsersUsherDesktop全栈15期day21 00.py"
6.打印进度条
import time
def progress(percent):
if percent > 1:
percent=1
print("
[%-50s] %d%%" % ("#"*int(50*percent),int(100 * percent)),end='')
# %-50s -代表左对齐 50 代表50个总共 %s 一个占位符
total_size = 1025
recv_size = 0
while recv_size < total_size:
# 每次下载1024
time.sleep(0.3)
recv_size+=1024
percent = recv_size / total_size
progress(percent)