time模块
作用
对时间进行操作的模块
方法
time.time() #获取当前的时间戳,从1970-1-1到现在的秒数
time.ctime(seconds) #返回时间字符串
time.localtime([seconds]) #返回时间元组
time.mktime(time_struct) #返回时间戳
time.asctime(time_struct) #返回时间字符串
time.sleep(seconds) #程序睡眠时间
time.perf_counter() 获取计时器的时间
time.clock() 返回当前cpu的时间
time.strftime(format[,time_struct]) #格式化时间
time.strptime(string,format) #转化成时间元组,注意前后对应
格式化时间的参数
符号 | 作用 |
---|---|
%Y | 年份,带上世纪 |
%y | 年份,不带世纪 |
%m | 月份 |
%d | 天数 |
%H | 小时 |
%M | 分钟 |
%S | 秒数 |
random(随机模块)
作用
产生随机
方法
random() 随机产生[0-1)件的小数
randint(a,b) 随机产生[a-b]间的数
randrange(start,stop[,step=1]) 随机产生数
shuffle(l) 打乱l
sample(iterable,k) 随机选择k个数
choice(iterable) 随机选择一个
choices(iterable,gailv,k) 设置概率,随机选取k个
uniform(a,b) 随机产生[a到b)之间的小数
os模块
作用
对系统文件进行操作的模块
方法
chdir(path) 改变工作目录
chmod(path,mode) 改变权限
close(fd) 关闭文件
getcwd() 获取当前的路径
listdir(path=None) 获取当前文件夹下所有的文件,返回列表
mkdir(path) 创建文件
makedirs(path) 递归创建文件
remove(path) 删除文件
rmdir(path) 删除目录
removedirs(path) 递归删除目录
renames(old,new) 改名字
stat(path) 返回状态信息
system() 运行系统的文件
os.path模块
作用
对系统路径进行操作的
方法
abspath(path) 获取当前路径的绝对路径
basename(path) 获取当前路径的主体部分,也就是最后一个
dirname(path) 获取除了主体的部分
exists(path) 判断当前路径存在不存在
getctime()filename) 获取文件的创建时间
getmtime() 获取文件的修改时间
getatime() 获取文件的访问时间
join(p1,p2) 连接路径,当p1绝对路径的时候
split(path) 划分主体和其余的
splitext(path) 划分成其余的和后缀名组成的元组
getsize(path) 获取文件的大小
isfile() 是不是文件
isdir() 是不是目录
isabs() 是不是绝对路径
samefile(p1,p2) 判断是不是同一文件