模块
一、time模块
时间戳(timestamp) :时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。
元组(struct_time) :struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天,夏令时)
- import time
- #1 ()返回当前时间戳
- print(time.time()/(365*24*60*60)+1970)
- #2018.8360429179454
-
- #2 localtime(secs)将一个时间戳转换为当前时区的struct_time,若secs参数为空,则以当前时间戳为准。
- print(time.localtime())
- #time.struct_time(tm_year=2018, tm_mon=10, tm_mday=21, tm_hour=11, tm_min=52, tm_sec=30, tm_wday=6, tm_yday=294, tm_isdst=0)
-
- #3 gmtime(secs)将一个时间戳转换为UTC时区(0时区)的struct_time。
- print(time.gmtime())
- #time.struct_time(tm_year=2018, tm_mon=10, tm_mday=21, tm_hour=4, tm_min=6, tm_sec=11, tm_wday=6, tm_yday=294, tm_isdst=0)
-
- #4 mktime(t)将一个struct_time转化为时间戳。
- print(time.mktime(time.localtime()))
- #1540094878.0
-
- #5 asctime([t])把一个表示时间的元组或者struct_time表示为这种形式:'Sun Jun 20 23:21:05 1993'。没有参数则默认以time.time()作为参数。
- print(time.asctime())
- #Sun Oct 21 12:10:20 2018
-
- #6 ctime([secs])把一个时间戳(按秒计算的浮点数)转化为这种形式'Sun Jun 20 23:21:05 1993'
- print(time.ctime(time.time()))
- #Sun Oct 21 12:23:49 2018
-
- #7 strftime(format[, t]) : 把一个代表时间的元组或者struct_time转化为格式化的时间字符串。如果t未指定,将传入time.localtime()
- print(time.strftime("%Y-%m-%d %X",time.localtime()))
- #2018-10-21 12:27:01
-
- #8 time.strptime(string[, format]把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。
- print(time.strptime('2018-10-21 12:27:01', '%Y-%m-%d %X'))
- #time.struct_time(tm_year=2018, tm_mon=10, tm_mday=21, tm_hour=12, tm_min=27, tm_sec=1, tm_wday=6, tm_yday=294, tm_isdst=-1)
二、random模块
- import random
- #1 random()输出0-1之间的随机数
- print(random.random())
- #0.6563316163409958
-
- #2 randint()输出一个范围内的随机整数
- print(random.randint(1,8))
- #3 choice()输出一个序列中的随机一个元素
- print(random.choice('hello'))
- print(random.choice(['hello',1,2,123]))
- #h 123
-
- #4 sample()以列表形式输出一个序列中的随机几个元素
- print(random.sample(['hello',1,2,123],2))
- #['hello', 2]
-
- #5 randrange()输出一个范围内的随机整数(不含尾)
- print(random.randrange(1,3))
- #2
-
- #验证码
- def v_code():
- code = ''
- for i in range(5):
- add_num = random.choice([random.randrange(1,10),chr(random.randrange(65,91))])
- code += str(add_num)
- print(code)
- v_code()
#N11QV
三、os模块
- import os
- print(os.getcwd()) # 获得当前工作目录
- print(os.chdir("dirname")) # 改变当前脚本的工作路径,相当于shell下的cd
- print(os.curdir) # 返回当前目录‘.'
- print(os.pardir) # 获取当前目录的父目录字符串名‘..'
- print(os.makedirs('dirname1/dirname2')) # 可生成多层递归目录
- print(os.removedirs('dirname1/dirname2')) # 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
- print(os.mkdir('test4')) # 生成单级目录;相当于shell中mkdir dirname
- print(os.rmdir('test4')) # 删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname
- print(os.listdir('/pythonStudy/s12/test')) # 列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
- print(os.remove('log.log')) # 删除一个指定的文件
- print(os.rename("oldname","newname")) # 重命名文件/目录)
- print(os.stat('/pythonStudy/s12/test')) # 获取文件/目录信息
- print(os.pathsep) # 输出用于分割文件路径的字符串';'
- print(os.name) # 输出字符串指示当前使用平台。win->'nt'; Linux->'posix'
- print(os.system(command='bash')) # 运行shell命令,直接显示
- print(os.environ) # 获得系统的环境变量
- print(os.path.abspath('/pythonStudy/s12/test')) # 返回path规范化的绝对路径
- print(os.path.split('/pythonStudy/s12/test')) # 将path分割成目录和文件名二元组返回
- print(os.path.dirname('/pythonStudy/s12/test')) # 返回path的目录。其实就是os.path.split(path)的第一个元素
- print(os.path.basename('/pythonStudy/s12/test')) # 返回path最后的文件名。如果path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素
- print(os.path.exists('test')) # 判断path是否存在
- print(os.path.isabs('/pythonStudy/s12/test')) # 如果path是绝对路径,返回True
- print(os.path.isfile('test')) # 如果path是一个存在的文件,返回True。否则返回False
- print(os.path.isdir('/pythonStudy/s12/test')) # 如果path是一个存在的目录,则返回True。否则返回False
- print(os.path.getatime('/pythonStudy/s12/test')) # 返回path所指向的文件或者目录的最后存取时间
- print(os.path.getmtime('/pythonStudy/s12/test')) # 返回path所指向的文件或者目录的最后修改时间
四、sys模块
- import sys
- print(sys.argv) # 命令行参数List,第一个元素是程序本身路径
- print(sys.exit(n)) # 退出程序,正常退出时exit(0)
- print(sys.version) # 获取python的版本信息
- print(sys.path) # 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
- print(sys.platform) # 返回操作平台的名称