Python入门学习-DAY17-常用模块

TIME模块

time模块
与时间相关的功能
在python中 时间分为3种
1.时间戳 timestamp 从1970 1 月 1日 到先在的秒数 主要用于计算两个时间的差
2.localtime 本地时间 表示的是计算机当前所在的位置
3.UTC 世界协调时间
时间戳 结构化 格式化字符

import time

# 获取时间戳 返回浮点型
print(time.time())

# 获取当地时间 返回的是结构化时间
print(time.localtime())

# 获取UTC时间 返回的还是结构化时间 比中国时间少8小时
print(time.gmtime())

# 将获取的时间转成我们期望的格式 仅支持结构化时间
print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()))

# 将格式化字符串的时间转为结构化时间 注意 格式必须匹配
print(time.strptime("2018-08-09 09:01:22","%Y-%m-%d %H:%M:%S"))

# 时间戳 转结构化
print(time.localtime(time.time()))

# 结构化转 时间戳
print(time.mktime(time.localtime()))

# sleep 让当前进程睡眠一段时间 单位是秒
# time.sleep(2)
# print("over")

# 不太常用的时间格式
print(time.asctime())
print(time.ctime())

DAETIME模块

"""
datetime
python实现的一个时间处理模块
time 用起来不太方便 所以就有了datetme
总结 datetime相比time 更灵活 更本土化

timedelta表示时间差
两个时间差可以 +-*/
时间差和datetime 可以+-


import datetime

# 获取时间 获取当前时间 并且返回的是格式化字符时间
print(datetime.datetime.now())

# 单独获取某个时间 年 月
d = datetime.datetime.now()
print(d.year)
print(d.day)

# 手动指定时间
d2 = datetime.datetime(2018,8,9,9,50,00)
print(d2)

# 计算两个时间的差 只能- 不能加+
print(d - d2)

# 替换某个时间
print(d.replace(year=2020))

# 表示时间差的模块 timedelta
print(datetime.timedelta(days=1))

t1 = datetime.timedelta(days=1)
t2 = datetime.timedelta(weeks=1)
print(t2 - t1)
# 时间差可以和一个datetime进行加减

 

RANDOM模块

"""
random 随机数相关模块
random 0-1 开闭
randint 0 - 3 开开
randrange 0 - 3 开闭
choice [1,2,32,3,2,"哈哈"] 随机选⼀个
sample([1,"a",["c","d"]],2) 随机选指定个数
uniform(1,3) 闭闭 浮点
shuffle(列表) 打乱顺序
"""
import random
# 0 - 1 随机浮点 不包含1
print(random.random())
print(random.randint(1,3))
print(random.randrange(1,3))
print(random.choices((1,2,3)))
print(random.sample([1,2,3],2))

l = [1,2,3,4,5]
print(random.shuffle(l))
print(l)


print(random.uniform(1,2))

# 生产验证码 整形和字符(全大写)随机组成 可以指定长度

def make_code(i):
  res = ""
  for j in range(i):
  # 随机0到9
  num = str(random.randint(0,9))
  c = chr(random.randint(65,90))
  s = random.choice([num,c])
  res += s
return res
print(make_code(4))

shutil模块

shutil 模块
用于简化文件操作 (文件的高级操作)
常用方法
copy
move
rm
make_archive

"""
import shutil
import zipfile,tarfile
# shutil.copyfileobj(open("a.txt","r",encoding="utf-8"),open("b.txt","w",encoding="utf-8"),length=1024)

# 利用shutil来创建压缩文件 仅支持 tar 和zip格式 内部调用zipFIle tarFIle模块实现
# shutil.make_archive("test","zip",root_dir="D:\脱产三期视频\Day17\代码")

# 解压zip
# z = zipfile.ZipFile(r"D:\脱产三期视频\Day17\test.zip")
# z.extractall()
# z.close()

# 解压tar
# t = tarfile.open(r"D:\脱产三期视频\Day17\test.tar")
# t.extractall()
# t.close()


# t = tarfile.open(r"D:\脱产三期视频\Day17\mytar.tar","w")
# t.add(r"D:\脱产三期视频\Day17\datetime_test.py")
# t.close()

 SYS模块

"""
sys模块
系统相关
一般用于设计脚本程序
常用方法
argv 获取cmd输入的参数


"""
import sys
print(sys.argv)

print(sys.platform)

# print(sys.exit(0))

模拟进度条

"""
当程序要进行耗时操作时例如 读写 网络传输
需要给用户提示一个进度信息
"[********* ]"
"[********* ]"
"[************ ]"
"[****************]"
"""
# print("[* ]")
# print("[** ]")

print("[%-10s]" % ("*" * 1))
print("[%-10s]" % ("*" * 2))
print("[%-10s]" % ("*" * 3))
print("[%-10s]" % ("*" * 4))

# 比例 0 - 1 0.5
def progress(percent,width=30):
  percent = percent if percent <= 1 else 1
  text = ("\r[%%-%ds]" % width) % ("*" * int((width * percent)))
  text = text + "%d%%"
  text = text % (percent * 100)
  print( text , end="")

# progress(0.5)


# 模拟下载
# 文件大小
import time
file_size = 10240
# 已经下载大小
cur_size = 0
while True:
  time.sleep(0.5)
  cur_size += 1021
  progress(cur_size / file_size)
  if cur_size >= file_size:
    print()
    print("finish")
    break

# print("%-%ds" % (30,"1"))
#


# # %-30s % * %-30s
# # %%-%ds % 20
#
# # %-20s % "1212"
# print("%%")


# print(("%%-%ds" % 20) % "*" )

OS模块

"""
OS模块
os表示操作系统相关

第一大快功能 就是围绕文件和目录的操作


"""
import os,sys
# print(os.getcwd())
# print(__file__)

print(os.stat("D:\脱产三期视频\Day17\datetime_test.py"))

print(sys.platform)
print(os.name)

print(os.system("dir"))

print(os.environ)


print(os.path.abspath(r"bbb.txt"))

print(os.path.join("C:","users","aaa.txt"))
print(os.path.normcase(r"/a/b/CSD"))
print(os.path.normpath(r"a\b\c\d\.."))

# 获取项目主目录

print(os.path.dirname(os.path.dirname(__file__)))


print(os.path.normpath(os.path.join(os.getcwd(), os.pardir)))

猜你喜欢

转载自www.cnblogs.com/xvchengqi/p/9448775.html