time模块
time模块应用
import time
print('1'.center(30,'-'))
print(time.time()) #时间戳
print('2'.center(30,'-'))
print(time.clock()) #计算CPU的执行时间
print('3'.center(30,'-'))
print(time.gmtime()) #结构化时间
print('4'.center(30,'-'))
print(time.localtime()) #本地时间
print('5'.center(30,'-'))
print(time.strftime('%Y-%m-%d %H:%M:%S')) #格式化输出时间
print('6'.center(30,'-'))
print(time.strptime('2020-06-25 18:25:12','%Y-%m-%d %H:%M:%S')) #格式化输出时间
print('7'.center(30,'-'))
a=time.strptime('2020-06-25 18:25:12','%Y-%m-%d %H:%M:%S') #将时间格式化编写给一个变量
print(a.tm_year) #变量调用方法,可以分别打印出年月日等时间信息
print(a.tm_mon)
print('8'.center(30,'-'))
print(time.ctime()) #打印当前时间,但是格式是固定的
print(time.ctime(1234567890)) #ctime后面的括号中可以写时间戳,根据这个时间戳计算出对应的年月日时间
print('9'.center(30,'-'))
print(time.mktime(time.localtime())) #将格式化的时间转换为时间戳
执行结果:
--------------1---------------
1593085086.9282165
--------------2---------------
3e-07
--------------3---------------
time.struct_time(tm_year=2020, tm_mon=6, tm_mday=25, tm_hour=11, tm_min=38, tm_sec=6, tm_wday=3, tm_yday=177, tm_isdst=0)
--------------4---------------
time.struct_time(tm_year=2020, tm_mon=6, tm_mday=25, tm_hour=19, tm_min=38, tm_sec=6, tm_wday=3, tm_yday=177, tm_isdst=0)
--------------5---------------
2020-06-25 19:38:06
--------------6---------------
time.struct_time(tm_year=2020, tm_mon=6, tm_mday=25, tm_hour=18, tm_min=25, tm_sec=12, tm_wday=3, tm_yday=177, tm_isdst=-1)
--------------7---------------
2020
6
--------------8---------------
Thu Jun 25 19:38:06 2020
Sat Feb 14 07:31:30 2009
--------------9---------------
1593085086.0
Process finished with exit code 0
time.strptime方法的应用:
datetime模块
示例:
import datetime
print(datetime.datetime.now())
执行结果:
2020-06-25 19:42:15.922311
Process finished with exit code 0
执行程序的时候,发现datetime.datetime.now()总是出现如下报错:
module 'datetime' has no attribute 'now'
出现这个报错的原因是该程序的文件名是datetime,修改文件名,即可正常执行
random随机数模块
应用示例:
import random
print('1'.center(30,'-'))
print(random.random()) #打印是的0-1的一个随机数
print('2'.center(30,'-'))
print(random.randint(1,8)) #打印一个范围内的随机整数包括范围边界值(也就是前面的1和8)
print('3'.center(30,'-'))
print(random.choice('yuyang')) #打印字符串中随机一个字母
print(random.choice([1,2,'yuyang','jiayanping'])) #打印列表中随机一个元素
print('4'.center(30,'-'))
print(random.sample([1,2,'yuyang','jiayanping'],2)) #打印列表中随机两个元素
print('5'.center(30,'-'))
print(random.randrange(1,3)) #打印[1,3)范围内随机数,左边能取到,右边取不到
执行结果:
--------------1---------------
0.13536658073320262
--------------2---------------
3
--------------3---------------
y
jiayanping
--------------4---------------
['yuyang', 'jiayanping']
--------------5---------------
2
Process finished with exit code 0
生成随机验证码的程序示例:
import random
def v_code():
code=''
for i in range(6):
add_num=random.randrange(10)
code += str(add_num)
print(code)
v_code()
执行结果:
756390
Process finished with exit code 0
上面这种方法生成的是全部为数字的验证码
那么生成数字和字母结合的验证码程序如下:
import random
def v_code():
code=''
for i in range(6): #这里循环6次表示生成6位数的验证码
if i == random.randint(0,9): #这里的if判断是如果i跟0-9的随机数相同,则打印数字,如果不同,则else打印字母
add=random.randrange(10)
else:
add=chr(random.randrange(65,90))
code += str(add)
print(code)
v_code()
更好的实现方式
def v_code():
code=''
for i in range(6):
add=random.choice([random.randrange(10),chr(random.randrange(65,90))])
code += str(add)
print(code)
v_code()
执行结果:
65EHTD
Process finished with exit code 0
8G47O1
Process finished with exit code 0
11L9E5
Process finished with exit code 0
OS模块
功能:调用操作系统
import os
os.getcwd() #获取当前工作目录,即当前python脚本工作的目录路径
os.chdir("dirname") #改变当前脚本工作目录,相当于shell下的cd
os.curdir #返回当前目录
os.pardir #获取当前目录的父目录字符串名
os.makedirs('dirname1/dirname2') #可生成多层递归目录,相当于shell中的mkdir -p
os.removedirs('dirname1') #若目录为空,则删除,并递归到上一级目录,若也为空,则删除。只能删除空目录
os.mkdir('dirname') #生成单级目录,相当于shell中的mkdir dirname
os.rmdir('dirname') #删除单级空目录,若目录部位空则无法删除,报错;相当于shell中rmdir dirname
os.listdir('dirname') #列出执行目录下的所有文件和子目录,包括隐藏文件,并以列表方式打开
os.remove() #删除一个文件,不能删除目录
os.rename('oldname','newname') #重命名文件/目录
os.stat('path/filename') #获取文件/目录信息
os.sep #输出操作系统特定的路径分隔符,win下为\\,Linux下为/
os.linesep #输出当前平台使用的行终止符,win下为\r\n,Linux下为\n
os.pathsep #输出用于分割文件路径的字符串
os.name #输出字符串只是当前使用平台。win->nt,linux->posix
os.system('bash command') #运行shell命令,直接显示
os.environ #获取系统环境变量
os.path.abspath(path) #返回path规范化的绝对路径,就是括号中输入相对路径,可以打印出该相对路径的绝对路径
os.path.split(path) #将path分割成目录和文件名二元组返回
os.path.dirname(path) #返回path最后的文件名,其实就是os.path.split(path)的第一个元素
os.path.basename(path) #返回path最后一个文件名。如何path以/或者\结尾,那么就会返回空值
os.path.exists(path) #如果path存在,返回True;反之,返回False
os.path.isabs(path) #如果path是绝对路径,返回True
os.path.isfile(path) #如果path是一个存在的文件,返回True,否则返回False
os.path.isdir(path) #如果path是一个存在的目录,则返回True,否则返回False
os.path.join(a,b) #用于路径拼接,路径拼接的时候不要使用+号
os模块调用程序执行示例:

os.stat()
print(os.stat('.\\ceshi.py'))
print(os.stat('..\\day17'))
info=os.stat('..\\day17')
print(info.st_size)
执行结果:
os.stat_result(st_mode=33206, st_ino=5066549580898642, st_dev=3403794469, st_nlink=1, st_uid=0, st_gid=0, st_size=720, st_atime=1593177306, st_mtime=1593177305, st_ctime=1593158375)
os.stat_result(st_mode=16895, st_ino=9007199254808218, st_dev=3403794469, st_nlink=1, st_uid=0, st_gid=0, st_size=4096, st_atime=1593162740, st_mtime=1593135399, st_ctime=1592993591)
4096
Process finished with exit code 0
该方法可以显示文件或者目录的基本信息,包括文件大小,修改时间,创建时间等等
sys模块
import sys
sys.argv #命令行参数list,第一个元素是程序本身路径
sys.exit(n) #退出程序,征程退出时,exit(0)
sys.version #获取python解释程序的版本信息
sys.maxint #最大的int值
sys.path #返回模块的搜索路径,初始化时使用Pythonpath环境变量的值
sys.path #添加自定义模块路径到环境变量path中
sys.platform #返回操作系统平台名称,常用于跨平台程序
sys.stdout.write('please') #终端上标准输出
val = sys.stdin.readline()[:-1]