python-常用库

import calendar
#calender :获取一年的日历字符串
#参数
#w=每个日期之间的间隔字符数
#l=每周所占用的行数
#c=每月之间的间隔字符数
cal=calendar.calendar(2017)
print(type(cal))
print(cal)


cal=calendar.calendar(2017,l=0,c=5)
print(cal)


#isleap:判断某一年是否是闰年
calendar.isleap(2000)

#leapdays:获取指定年份之间的闰年个数
calendar.leapdays(2001,2018)
help (calendar.leapdays)

#month()获取某个月的日历字符串
#格式:calendar.month(2018,3)
#回值:月日历的字符串
m3=calendar.month(2018,3)
print(m3)

#monthrange()获取一个月的周几开始集合天数
#格式:calendar.monthrange(年,月)
#回值:元组(周几开始,总天数)
w,t=calendar.monthrange(2017,3)
print(w)
print(t)


#monthcalendar()返回一个月每天的矩阵列表
#格式:calendar.monthcalendar(年,月)
#回值:二级列表
#注意:矩阵中没有天数用0表示
m=calendar.monthcalendar(2018,3)
print(type(m))
print(m)


#prcal:print calendar直接打印日历
calendar.prcal(2018)
help(calendar.prcal)

#prmonth()直接打印整个月的日历
#格式:calendar.prmonth(年,月)
#返回值:无
calendar.prmonth(2018,3)

#weekday()获取周几
#格式:calendar.weekday(年,月,日)
#返回值:周几对应的数字
calendar.weekday(2018,3,26)

#time模块

   #时间戳

      -一个时间表示,根据不同的语言,可以是整数或者浮点数

      -是从1970年1月1日0时0分0秒到现在经历的秒数

      -如果表示的时间是1970年以前或者太遥远的未来,可能出现异常

      -32位操作系统能够支持到2038年

   ·UTC时间

      -世界协调时间

      -中国时间是UTC+8东八区

   

#夏令时

      -在夏天的时候将时间调快一个小时

#时间元组

   -一个包含时间内容的普通元组

   http:#

import time
#时间模块属性
#timezone:当前时区和UTC时间相差的秒数,没有夏令时的情况下的间隔
#altzone:获取当前时区与UTC时间相差的秒数,在夏令时的情况下
#daylight:测当前是否是夏令时状态,0表示是
print(time.timezone)
print(time.altzone)
print(time.daylight)

#得到时间戳
time.time()

#localtime(),得到当前时间的时间结构
#可以通过点号操作符得到相应的属性元素的内容
t=time.localtime()
print(t)


#asctime()返回元组的正常字符串化之后的时间格式
#格式:time.asctime(时间元组)
#返回值:字符串Tue Jun 6:11:11:00 2017
tt=time.asctime(t)
print(type(tt))
print(tt)


#ctime:获取字符串化的当前时间
t=time.ctime()
print(type(t))
print(t)


#mktime()使用时间元组获取对应的时间戳
#格式:time.mktime(时间元组)
#返回值:浮点数时间戳
lt=time.localtime()
ts=time.mktime(lt)
print(type(ts))
print(ts)



#clock:获取CPU时间
#sleep:使程序进入睡眠,n秒后继续
for i in range(10):
    print(i)
    time.sleep(1)








import time
#时间模块属性
#timezone:当前时区和UTC时间相差的秒数,没有夏令时的情况下的间隔
#altzone:获取当前时区与UTC时间相差的秒数,在夏令时的情况下
#daylight:测当前是否是夏令时状态,0表示是
print(time.timezone)
print(time.altzone)
print(time.daylight)

#得到时间戳
time.time()

#localtime(),得到当前时间的时间结构
#可以通过点号操作符得到相应的属性元素的内容
t=time.localtime()
print(t)


#asctime()返回元组的正常字符串化之后的时间格式
#格式:time.asctime(时间元组)
#返回值:字符串Tue Jun 6:11:11:00 2017
tt=time.asctime(t)
print(type(tt))
print(tt)


#ctime:获取字符串化的当前时间
t=time.ctime()
print(type(t))
print(t)


#mktime()使用时间元组获取对应的时间戳
#格式:time.mktime(时间元组)
#返回值:浮点数时间戳
lt=time.localtime()
ts=time.mktime(lt)
print(type(ts))
print(ts)



#clock:获取CPU时间
#sleep:使程序进入睡眠,n秒后继续
for i in range(10):
    print(i)
    time.sleep(1)

 

import time
#strftime()
t=time.localtime()
ft=time.strftime("%Y年%m月%d日 %H:%M",t)
print(ft)

#datetime模块
#datetime提供日期和时间的运算和表示
#datetime.date:一个理想和的日期,提供year,month,day属性
import datetime
print(datetime.date(2018,8,8))
dt=datetime.date(2018,8,8)
print(dt)
print(dt.year)
print(dt.day)
print(dt.month)

#datetime.time提供一个理想和时间,提供hour,minute,sec,microsec等内容
#datetime.datetime提供日期跟时间的组合
#datetime.timedelta提供一个时间差,时间长度
from datetime import datetime
#常用类方法;
#today
#now
#utcnow
#fromtimestamp从时间戳中返回本地时间
dt=datetime(2018,8,8)
print(dt.today())
print(dt.now())
print(dt.fromtimestamp(time,time()))

#datetime.timedelta
#表示一个时间间隔
from datetime import datetime,timedelta
t1=datetime.now()
print(t1)
print(t1.strftime("%Y-%m-%d %H:%M:%S"))

#timeit-时间测量工具
def p():
    time.sleep(3.6)
t1=time.time()
p()
print(time.time()-t1)



from datetime import datetime as at
print(at.now())

#OS-与操作系统相关

-跟操作系统相关,主要是文件操作

-与系统相关的操作,主要包含在三个模块里

   ·os,操作系统目录相关

   ·os.path,系统路径相关操作

   ·shutil,高级文件操作,目录树的操作,文件赋值,删除,移动

-路径

   ·绝对路径:总是从根目录上开始

   ·相对路径:基本上以当前环境为开始的一个相对的地方

#os模块
#getcwd()获取当前的工作目录
#格式:os.getcwd()
#返回值:当前工作目录的字符串
#当前工作目录就是程序在进行文件相关操作,默认查找文件的目录
import os
mydir=os.getcwd()
print(mydir)

#chdir()改变当前的工作目录
#change directory
#格式:os.chdir(路径)
#返回值:无

#os.chdir('/home/tlxy')
#mydir=os.getcwd()
#print(mydir)


#listdir()获取一个目录中所有的子目录和文件名称列表
#格式:os.listdir(路径)
#返回值:所有子目录和文件名称的列表

ld=os.listdir()
print(ld)

#makedirs()递归创建文件夹
#格式:os.makedirs(递归路径)
#返回值:无
#递归路径:多个文件夹层层包含的路径就是递归路径,例如a/b/c...
#rst=os.makedirs("dana")
#print(rst)


#system()运行系统shell命令
#格式:os。system(系统命令)
#返回值:打开一个shell或者终端界面
#一般推荐使用subprocess代替
#ls是列出当前文件和文件夹的系统命令
rst=os.system("ls")
print(rst)


#getenv()获取指定的系统环境变量值
#相应的还有putenv
#格式:os。getenv('环境变量名')
#返回值:指定环境变量名对应的值
rst=os.getenv("PATH")
print(rst)


#exit()退出当前程序
#格式:exit()
#返回值:无


#值部分
#-os.curdir:currentn dir,当前目录
#-os.pardir:parent dir,父级目录
#-os.sep:当前系统的路径分隔符-windows:"\"-linus:"/"
#-os.linesep:当前系统的路径分隔符-windows:"\r\n"
#-os.name:当前系统名称
print(os.pardir)
print(os.curdir)
print(os.sep)
print(os.linesep)


#linus操作系统名称为posix
print(os.name)
import os.path as op

#abspath()将路径化为绝对路径
#abselute绝对
#格式:os.path.abspath('路径')
#返回值:路径的绝对路径形式

#Linus中点号代表当前目录,双点代表父目录
absp=op.abspath(".")
print(absp)

#basename()获取路径中的文件名部分
#格式:os.path.basename(路径)
#返回值:文件名字符串
bn=os.basename("/home/tlxy")
print(bn)
bn=os.basename("/home/tlxy/dana.haha")
print(bn)

#join()将多个路径拼合在一起
#格式:os.path.join(路径1,路径2...)
#返回值:组合之后的新路径字符串
bd="/home/tlxy"
fn="dana.haha"
p=op.join(bd,fn)
print(p)


#split()将路径分割成文件夹部分和当前文件夹部分
#格式:os.path.split(路径)
#返回值:路径和文件名组成的元组
t=os.split("/home/tlxy/dana.haha")
print(t)
d,p=op.split("/home/tlxy/dana.haha")
print(d,p)

#isdir()检测是否是目录
#格式:os.path.isdir(路径)
#返回值:布尔值
rst=op.exists("/home")
rst

#exists()检测是否是目录
#格式:os.path.exists(路径)
#返回值:布尔值
e=op.exists("/home")
e




#shutil模块

-copy()复制文件

-格式:shutil.copy(来源路径,目标路径)

-返回值:返回目标路径

-拷贝的同时,可以给文件重命名

import shutil
rst=shutil.copy("/home/tlxy/dana.haha","/home/tlxy/haha,haha")
print(rst)

#copyfile()将一个文件中的内容复制在另一个文件当中
#格式:shutil.copyfile(路径)
#返回值:无
rst=shutil.copyfile("/home/tlxy/haha,haha")
print(rst)

#move()移动文件/文件夹
#格式:shutil.move(源路径,目标路径)
rst=shutil.move("/home/tlxy/haha,haha")
print(rst)

#归档和压缩

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

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

#make_archive()归档操作
#格式:shutil.make_archive('归档之后的目录和文件名','后缀','需要归档的文件')
#返回值:归档之后的地址
help(shutil.make_archive)
rst=shutil.make_archive("/python/dd","me","/python/csdn")
print(rst)

#unpack_archive()解包操作
#格式:shutil.unpack_archive('归档文件地址','解包之后的地址')
#返回值:解包之后的地址

#zip-压缩包-木块名称叫做zipfile
#zipfile.ZipFile(file[,mode[,compression[,allowZip64]]])
#创建一个ZipFile对象,表示一个zip文件,参数file表示文件的路径或类文件对象
zf=zipfile.ZipFile("/home/tlxy/tg.zip")

#ZipFile.getinfo(name):
#获取zip文档内指定文件的信息。返回一个zipfile.ZipInfo对象,它包括文件的详细信息
rst=zf.getinfo("dsfs")
print(rst)

#ZipFile.extractall([path[,members[,pwd]]])
#解压zip文档中的所有文件到当前目录。参数members的默认值为zip文档内所有文件名称列表
rst=zf.extractall("/home/python/csdn")
print(rst)

 

#random

-随机数

-所有随机模块都是伪随机

import random
#random()获取0-1之间的随机小数
#格式:random.random()
#返回值:随机0-1之间的小数
print(random.random())

#choice()随机返回序列中的某个值
#格式:random.choice(序列)
#返回值:序列中的某个值
l=[str(i)+"haha" for i in range(10)]
print(l)
rst=random.choice(l)
print(rst)

#shuffle()随机打乱列表
#格式:random.shuffle(列表)
#返回值:打乱顺序之后的列表
l1=[i for i in range(10)]
print(l1)
l2=random.shuffle(l1)
print(l2)
help(random.shuffle)


#randint(a,b):返回一个a到b之间的随机整数,包含a和b
print(random.randint(0,100))

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值