time模块主要是有三个方向
time.time()获得时间戳
time.localtime()获得时间的结构数据
time.strftime()获得的是格式化的字符串
具体使用是
>>> import time
>>> time.time()
1493188234.6272
>>> time.localtime()
time.struct_time(tm_year=2017, tm_mon=4, tm_mday=26, tm_hour=14, tm_min=30, tm_sec=44, tm_wday=2, tm_yday=116, tm_isdst=0)
>>> time.strftime("%Y-%m-%d %X")
'2017-04-26 14:31:38'
>>>
三种形式的相互转换
时间戳 --> 时间结构
localtime( 时间戳 ) 不写时间戳默认就是获取当前的时间戳
gmtime( 时间戳 ) 这个是格林威治时间
>>> time.localtime( time.time() )
time.struct_time(tm_year=2017, tm_mon=4, tm_mday=26, tm_hour=14, tm_min=40, tm_sec=13, tm_wday=2, tm_yday=116, tm_isdst=0)
>>> time.localtime( 237849 )
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=4, tm_hour=2, tm_min=4, tm_sec=9, tm_wday=6, tm_yday=4, tm_isdst=0)
>>> time.gmtime( time.time() )
time.struct_time(tm_year=2017, tm_mon=4, tm_mday=26, tm_hour=6, tm_min=40, tm_sec=50, tm_wday=2, tm_yday=116, tm_isdst=0)
>>>
时间结构 --> 格式化字符串
strftime( 显示的字符串的格式, 时间结构 ) 第二参数不写默认就是当前时间点的时间结构
>>> time.strftime("%Y-%m-%d", time.localtime(time.time()+24*3600))
'2017-04-27'
>>>
格式化字符串 -- > 时间结构
strptime( 某种格式的字符串, 字符串格式 )
>>> time.strptime('2017-04-27', '%Y-%m-%d' )
time.struct_time(tm_year=2017, tm_mon=4, tm_mday=27, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=117, tm_isdst=-1)
>>>
格式化格式:
%Y Year with century as a decimal number.
%m Month as a decimal number [01,12].
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%M Minute as a decimal number [00,59].
%S Second as a decimal number [00,61].
%z Time zone offset from UTC.
%a Locale's abbreviated weekday name.
%A Locale's full weekday name.
%b Locale's abbreviated month name.
%B Locale's full month name.
%c Locale's appropriate date and time representation.
%I Hour (12-hour clock) as a decimal number [01,12].
%p Locale's equivalent of either AM or PM.
时间结构 --> 时间戳
mktime( 时间结构 )
>>> time.mktime( time.strptime('2017-04-27', '%Y-%m-%d' ) )
1493222400.0
>>>
astime() 根据时间结构获得指定格式的字符串
ctime() 根绝时间戳获得指定格式的字符串
>>> time.asctime(time.localtime(312343423))
'Sun Nov 25 10:03:43 1979'
>>> time.ctime(312343423)
'Sun Nov 25 10:03:43 1979'
2 random模块
random.random() 获得 [0,1) 范围内的浮点数
random.randint(m, n) 获得 [m, n] 之间的整数
random.randrange(m, n) 获得 [m, n) 之间的整数
random.uniform(m,n) 获得 (m, n) 之间的浮点数
random.choice( 序列 ) 在序列中随机选取一个返回
random.sanple( 序列, 个数 ) 在序列中随机选取n个返回
random.shuffke( 序列 ) 打乱序列顺序, 对原序列修改
3 hashlib模块
基本使用方式
先通过hashlib下的MD5等函数返回一个对象, 可以传入一个byte类型的数据来进行加盐操作
该对象可以通过update()来将传入的byte类型的数据加密
可以通过hexdigest()来获取加密后的结果
具体如下
m = hashlib.md5()
m.update("hello".encode("utf8"))
print( m.hexdigest() )
#5d41402abc4b2a76b9719d911017c592
其中, update可以多次使用
m = hashlib.md5()
m.update("hello".encode("utf8"))
m.update("hello".encode("utf8"))
print( m.hexdigest() )
m1 = hashlib.md5()
m1.update("hellohello".encode("utf8"))
print( m1.hexdigest() )
# 23b431acfeb41e15d466d75de822307c
# 23b431acfeb41e15d466d75de822307c
这个特性可以帮助加密较大或者更大的数据
加盐操作其实是在内容后拼接一些数据
4 os模块
os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径
os.chdir("dirname") 改变当前脚本工作目录;相当于shell下cd
os.curdir 返回当前目录: ('.')
os.pardir 获取当前目录的父目录字符串名:('..')
os.makedirs('dirname1/dirname2') 可生成多层递归目录
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下为"\t\n",Linux下为"\n"
os.pathsep 输出用于分割文件路径的字符串 win下为;,Linux下为:
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.split(path)的第二个元素
os.path.exists(path) 如果path存在,返回True;如果path不存在,返回False
os.path.isabs(path) 如果path是绝对路径,返回True
os.path.isfile(path) 如果path是一个存在的文件,返回True。否则返回False
os.path.isdir(path) 如果path是一个存在的目录,则返回True。否则返回False
os.path.join(path1[, path2[, ...]]) 将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
os.path.getatime(path) 返回path所指向的文件或者目录的最后存取时间
os.path.getmtime(path) 返回path所指向的文件或者目录的最后修改时间
os.path.getsize(path) 返回path的大小
5 sys模块
sys.argv 命令行参数List,第一个元素是程序本身路径
sys.exit(n) 退出程序,正常退出时exit(0)
sys.version 获取Python解释程序的版本信息
sys.path 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
sys.platform 返回操作系统平台名称