time与datetime模块
# 时间模块优先掌握的操作
#一:time
import time
# 时间分为三种格式:
# 1、时间戳:从1970年到现在经过的秒数
# 作用:用于时间间隔的计算
# print(time.time())
# 2、按照某种格式显示的时间:2020-03-30 11:11:11
# 作用:用于展示时间
# print(time.strftime('%Y-%m-%d %H:%M:%S %p'))
# print(time.strftime('%Y-%m-%d %X'))
# 3、结构化的时间
# 作用:用于单独获取时间的某一部分
# res=time.localtime()
# print(res)
# print(res.tm_year)
# print(res.tm_yday)
#二:datetime
import datetime
# print(datetime.datetime.now())
# print(datetime.datetime.now() + datetime.timedelta(days=3))
# print(datetime.datetime.now() + datetime.timedelta(weeks=1))
# 时间模块需要掌握的操作
# 1、时间格式的转换
# struct_time->时间戳
import time
# s_time=time.localtime()
# print(time.mktime(s_time))
# 时间戳->struct_time
# tp_time=time.time()
# print(time.localtime(tp_time))
# 补充:世界标准时间与本地时间
# print(time.localtime())
# print(time.gmtime()) # 世界标准时间,了解
# print(time.localtime(333333333))
# print(time.gmtime(333333333))
# struct_time->格式化的字符串形式的时间
# s_time=time.localtime()
# print(time.strftime('%Y-%m-%d %H:%M:%S',s_time))
# print(time.strptime('1988-03-03 11:11:11','%Y-%m-%d %H:%M:%S'))
# !!!真正需要掌握的只有一条:format string<------>timestamp
# '1988-03-03 11:11:11'+7
# format string--->struct_time--->timestamp
# struct_time=time.strptime('1988-03-03 11:11:11','%Y-%m-%d %H:%M:%S')
# timestamp=time.mktime(struct_time)+7*86400
# print(timestamp)
# format string<---struct_time<---timestamp
# res=time.strftime('%Y-%m-%d %X',time.localtime(timestamp))
# print(res)
# time.sleep(3)
# 了解知识
# import time
# print(time.asctime())
# import datetime
# print(datetime.datetime.now())
# print(datetime.datetime.utcnow())
# print(datetime.datetime.fromtimestamp(333333))
random模块
import random
# print(random.random()) #(0,1)----float 大于0且小于1之间的小数
# print(random.randint(1, 3)) # [1,3] 大于等于1且小于等于3之间的整数
# print(random.randrange(1, 3)) # [1,3) 大于等于1且小于3之间的整数
#
# print(random.choice([111, 'aaa', [4, 5]])) # 1或者23或者[4,5]
#
# print(random.sample([111, 'aaa', 'ccc','ddd'],2)) # 列表元素任意2个组合
#
# print(random.uniform(1, 3)) # 大于1小于3的小数,如1.927109612082716
#
# item = [1, 3, 5, 7, 9]
# random.shuffle(item) # 打乱item的顺序,相当于"洗牌"
# print(item)
# 应用:随机验证码
# import random
#
# res=''
# for i in range(6):
# 从26大写字母中随机取出一个=chr(random.randint(65,90))
# 从10个数字中随机取出一个=str(random.randint(0,9))
#
# 随机字符=random.choice([从26大写字母中随机取出一个,从10个数字中随机取出一个])
# res+=随机字符
# import random
#
# def make_code(size=4):
# res=''
# for i in range(size):
# s1=chr(random.randint(65,90))
# s2=str(random.randint(0,9))
# res+=random.choice([s1,s2])
# return res
#
# print(make_code(6))
os模块
# import os
# 获取某一个文件夹下所有的子文件以及子文件夹的名字
# res=os.listdir('.')
# print(res)
#
# size=os.path.getsize(r'/Users/linhaifeng/PycharmProjects/s14/day22/01 时间模块.py')
# print(size)
# os.remove() 删除一个文件
# os.rename("oldname","newname") 重命名文件/目录
# 应用程序----》"ls /"
# os.system("ls /")
# 规定:key与value必须都为字符串
# os.environ['aaaaaaaaaa']='111'
# print(os.environ)
# print(os.path.dirname(r'/a/b/c/d.txt'))
# print(os.path.basename(r'/a/b/c/d.txt'))
# print(os.path.isfile(r'笔记.txt'))
# print(os.path.isfile(r'aaa'))
# print(os.path.isdir(r'aaa'))
# print(os.path.join('a','/','b','c','d'))
# 推荐用这种
# BASE_DIR=os.path.dirname(os.path.dirname(__file__))
# print(BASE_DIR)
# BASE_DIR=os.path.normpath(os.path.join(
# __file__,
# '..',
# '..'
# ))
# print(BASE_DIR)
# 在python3.5之后,推出了一个新的模块pathlib
from pathlib import Path
# res = Path(__file__).parent.parent
# print(res)
# res=Path('/a/b/c') / 'd/e.txt'
# print(res)
# print(res.resolve())
sys模块
import sys
# python3.8 run.py 1 2 3
# sys.argv获取的是解释器后参数值
# print(sys.argv)
# src_file=input('源文件路径: ').strip()
# dst_file=input('目标文件路径: ').strip()
#
# src_file=sys.argv[1]
# dst_file=sys.argv[2]
# # 判断
#
# with open(r'%s' %src_file,mode='rb') as read_f,\
# open(r'%s' %dst_file,mode='wb') as write_f:
# for line in read_f:
# write_f.write(line)
# python3.8 run.py src_file dst_file
# print('[%-50s]' %'#')
# print('[%-50s]' %'##')
# print('[%-50s]' %'###')
# import time
#
# res=''
# for i in range(50):
# res+='#'
# time.sleep(0.5)
# print('\r[%-50s]' % res,end='')
# import time
#
# def progress(percent):
# if percent > 1:
# percent = 1
# res = int(50 * percent) * '#'
# print('\r[%-50s] %d%%' % (res, int(100 * percent)), end='')
#
# recv_size=0
# total_size=1025011
#
# while recv_size < total_size:
# time.sleep(0.01) # 下载了1024个字节的数据
#
# recv_size+=1024 # recv_size=2048
#
# # 打印进度条
# # print(recv_size)
# percent = recv_size / total_size # 1024 / 333333
# progress(percent)