python学习23之标准库

使用模块的时候都要先导入(string是特例)

1. calendar

跟日历相关的模块

import calendar

#calendar
import calendar
'''
calendar.calendar(year,w=2,l=1,c=6)
返回一个多行字符串格式的year年年历,
3个月一行,间隔距离为c。每日宽度间隔为w字符。
每行长度为21* W+18+2* C。
l是每星期行数。
'''
print(calendar.calendar(2018))
#isleap:判断某一年是否是闰年
print(calendar.isleap(2018))
#leapdays:获取指定年份之间的闰年个数
print(calendar.leapdays(1998,2080))
#month():获取指定某个月的日历字符串
print(calendar.month(2018,6))
#monthrange():获取一个月是周几开始和总天数,返回一个元组(周几开始,总天数)
#0表示周一,6表示周日
#monthcalendar():返回一个月每天的矩阵列表,矩阵中没有的天数用0表示
print(calendar.monthcalendar(2018,8))
#prcal:print calendar:直接打印日历
print(calendar.prcal(2018))
#prmonth:直接打印整个月的日历
print(calendar.prmonth(2018,3))
#weekday:获取周几
print(calendar.weekday(2018,6,9)) #返回0-6代表周一到周日

2. time模块

#time模块
'''
时间戳:一个时间表示,根据不同语言,可以是整数或者是浮点数
从1970年1月1日0时0分0秒到现在经历的秒数
如果标识的时间是1970年以前或者太遥远的未来,可能会出现异常
32位操作系统可以支持到2038年

UTC时间:世界协调时间,以英国格林尼治天文所在地区的时间作为参考的时间,也叫做世界标准时间
中国式UTC+8 东八区

时间元组:一个包含时间内容的普通元组
'''
#导入时间模块
import time
#时间模块的属性
#timezone:当前时区和UTC相差的秒数,在没有夏令时的情况下的间隔
#altzone:当前时区和UTC相差的秒数,在有夏令时的情况下的间隔
print(time.timezone)#东八区是-28800
#time():得到时间戳
print(time.time())
#asctime() 返回元组的正常字符串化之后的时间格式
print(time.localtime())
print(time.asctime())
#ctimeL:获取字符串化的当前时间
t=time.ctime()
print(t)
#mktime: 使用时间元组获取对应的时间戳
lt=time.localtime()
ts=time.mktime(lt)
print(type(ts))  #float
print(ts)
#clock:获取CPU时间 3.0-3.3版本使用
#sleep:使程序进入睡眠,n秒后继续
# for i in range(10):
#     print(i)
#     time.sleep(1) #1秒打印一个
#strftime:将时间元组转化为自定义的字符串格式
t1=time.localtime()
ft=time.strftime("%Y{y}%m{m}%d{d} %H:%M:%S",t1).format(y='年',m='月',d='日')
print(ft)

3. datetime模块

提供日期和时间的运算和表示

4. OS模块

操作系统相关

#os
import os
print(os.getcwd()) #返回当前工作路径的字符串
#chdir() 改变当前的工作目录
#listdir():返回一个目录中的所有子目录和文件的名称列表
#makedirs() 递归创建文件夹
#system():运行系统shell命令
# print(os.system("ping 127.0.0.1"))
#getenv():获取指定的系统环境变量值
print(os.getenv('PATH'))
#exit():退出当前程序
'''
os.curdir:
当前目录
os.pardir:父母目录
os.sep:当前系统的路径分隔符
os.linesep:当前系统的路径分隔符
os.name:当前系统名称
'''
print(os.pardir)

5. OS.path模块:与路径相关

#os.path
import os.path
#abspath() 将路径转化为绝对路径
absp=os.path.abspath('.')
print(absp)
#basename():获取路径中的文件名部分
#join():将多个路径拼合成一个路径
#split:将路径切割成文件夹部分和当前文件部分
#isdir():检测是否是目录
#exists:检测文件或目录是否存在

6. shutil模块

import shutil
#copy 复制文件 shutil.copy(来源路径,目标路径)
#copyfile 将一个文件中的内容复制到另一个文件当中
#move:移动文件/文件夹

归档和压缩:

归档:将多个文件或者文件夹合并到一个文件当中

压缩:用算法把多个文件或者文件夹无损或者有损合并到一个文件当中

make_archive()归档操作

make_archive(‘归档之后的目录和文件名’,’后缀’,’需要归档的文件夹’)

 

unpack_archive() 解包操作

shutil.unpack_archive(‘归档文件地址’,’解包之后的地址’)

 

7. zip—压缩包

import zipfile

创建一个zipfile对象,表示一个zip文件:zipfile.ZipFile(“路径/文件名.zip”)

ZipFile.getinfo(“文件名”) 查看zip文档内指定文件的信息

ZipFile.namelist() 查看zip文档内所有文件的名称列表

8. random模块

#random
import random
'''
随机数
所有的随机模块都是伪随机
random():获取0-1的随机小数
choice:随机返回序列中的某个值
shuffle:随机打乱列表
randint(a,b):返回一个a到b之间的随机整数,包含a和b
'''

print(random.random())

l=[i for i in range(1,10)]
print(l)
print(random.choice(l))

random.shuffle(l)
print(l)

9. math模块

#math
import math
#浮点数取整
print(math.trunc(3.9))
#取大整数
print(math.ceil(3.4))
#四舍五入 round不是math模块里的方法,而是内置函数
print(round(3.6))
#对元组每一个元素求和,fsum返回浮点数,sum返回整型
t_nums=(1,2,3)
print(math.fsum(t_nums))
print(sum(t_nums))
#求绝对值:fabs 返回浮点数 abs 返回整型(但如果对浮点型求绝对值,返回浮点型)
print(math.fabs(-3.5))
print(abs(-3.5))

posted @ 2020-04-01 20:40  程序员王不错  阅读(268)  评论(0)    收藏  举报