Python 模块(Module),是一个 Python 文件,以 .py 结尾,包含了 Python 对象定义和Python语句。
模块让你能够有逻辑地组织你的 Python 代码段。
把相关的代码分配到一个模块里能让你的代码更好用,更易懂。
模块能定义函数,类和变量,模块里也能包含可执行的代码。
模块的分类1、标准库2、开源模块3、时间模块 time 和 datatime 时间(1)格式格式化的字符串的形式(2)时间戳即秒数 time.time()1970年月日时-目前的秒的时间差 (3)元祖struct_time(tuple)共九个元素 time.localtime()年/月/日/时/分/秒/周几/年中的第几天/时区(中国东八区utc+8)把时间戳转换元祖import time,datetimetime.gmtime()#当前的标准时间utc时区time.localtime()#本地时区时间x=time.localtime()print(x)# print("this is 1973 da %d"%x.tm_yday)#元祖转时间戳print(time.mktime(x))#time.mktime()往回转print(time.ctime())#时间戳转字符串#元祖转字符串print(time.strftime("%Y-%m-%d %H:%M:%S",x))print(time.asctime())#如果不传参数会默认传localtime,当前时间#字符串转元祖print(time.strptime('1973-11-27 09:52:03',"%Y-%m-%d %H:%M:%S"))#必须一一对应字符串转时间戳#print(help(time.asctime))print(time.asctime())print(datetime.datetime.now())#获取当前时间print(datetime.datetime.now()+datetime.timedelta(-3))#当前时间减3天 默认时间为天print(datetime.datetime.now()+datetime.timedelta(hours=3))#当前时间+3小时print(datetime.datetime.now()+datetime.timedelta(minutes=30))#当前时间+30分random模块,取随机值import randomprint(random.random())#不能指定区间,只有0-1的浮点数print(random.randint(1,3))#有范围的传值print(random.randrange(1,3))#不包含3print(random.choice('hello'))#任意传参-字符串\元祖|字典print(random.sample('hello',2))#前面数列,后面位数,'hello',2表示字符串中任意去两位print(random.uniform(1,3))#有区间的浮点数l=[1,2,3,4,5,6]print(l)random.shuffle(l)print(l)os模块 属于系统模块import osos.getcwd()#获取当前操作目录os.chdir('C:\\Uses')#切换目录os.makedirs(r'C:\a\b\c\d')#创建目录import sys #系统相关的信息模块sys:sys.argv #是一个 list,包含所有的命令行参数.sys.stdout.stdin#表示标准输入输出的文件对象.sys.stderr #分别表示错误输出的文件对象.sys.stdin.readline() #从标准输入读一行 sys.stdout.write("a") 屏幕输出asys.exit #相当于(exit_code) 退出程序sys.modules # 是一个dictionary,表示系统中所有可用的modulesys.platform #得到运行的操作系统环境sys.path #是一个list,指明所有查找module,package的路径.操作系统相关的调用和操作: import osos.environ #一个dictionary 包含环境变量的映射关系os.environ["HOME"] #可以得到环境变量HOME的值os.chdir(dir) #改变当前目录 os.chdir('d:\\outlook') #注意windows下用到转义os.getcwd() #得到当前目录os.getegid() #得到有效组id os.getgid() 得到组idos.getuid() #得到用户id os.geteuid() 得到有效用户idos.getgruops() #得到用户组名称列表os.getlogin() #得到用户登录名称os.getenv #得到环境变量os.putenv #设置环境变量os.umask #设置umaskos.system(cmd) #利用系统调用,运行cmd命令转载于:https://www.cnblogs.com/buildydream/p/10246797.html