1 模块介绍
1.1 模块定义:
模块:用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件(文件名:test.py,对应模块名是test)
包:本质是一个目录(必须带一个__init__.py的文件),从逻辑上组织模块python
1.2 导入:
import test1,test2
from test import *
from test import name,def1
from test import def1 as def1_impshell
1.3 import的本质:()
导入模块的本质就是把python文件解释一遍
(import test ---> test = 'test.py all code')
(from test import name -->name = 'code')
导入包的本质执行该包下面的__init__.py文件bash
1.4 导入优化
from test import def1 as fun1
1.5 模块的分类:
1.5.1 标准库
1.5.2 三方开源模块
1.5.3 自定义模块app
2 内置模块
2.1 time 模块
struct_tuple:
time.struct_time(tm_year=2018, tm_mon=11, tm_mday=10, tm_hour=17, tm_min=49, tm_sec=0, tm_wday=5, tm_yday=314, tm_isdst=-1)dom
strftime():
%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.函数
2.2 datetime优化
datetime.datetime.now()---->
datetime.datetime(2018, 11, 10, 18, 23, 42, 407988)ui
timedelta():
+datetime.timedelta(3)
+datetime.timedelta(hours = 5)
练习代码:spa
__author__ = 'admin' import time,datetime x = time.time() y = time.localtime() print(x,'\n',y) # 1541846184.9383597 ; time.struct_time(tm_year=2018, tm_mon=11, tm_mday=10, tm_hour=18, tm_min=36, tm_sec=24, tm_wday=5, tm_yday=314, tm_isdst=0) print(time.mktime(y)) #1541846184.0 print(time.gmtime(x)) #time.struct_time(tm_year=2018, tm_mon=11, tm_mday=10, tm_hour=10, tm_min=36, tm_sec=24, tm_wday=5, tm_yday=314, tm_isdst=0) print(time.strftime('%Y-%m-%d %H:%M:%S',y)) #2018-11-10 18:39:18 print(time.strptime('2018-11-10 18:39:18','%Y-%m-%d %H:%M:%S')) #time.struct_time(tm_year=2018, tm_mon=11, tm_mday=10, tm_hour=18, tm_min=39, tm_sec=18, tm_wday=5, tm_yday=314, tm_isdst=-1) print(time.ctime(x)) #Sat Nov 10 18:41:24 2018 print(time.asctime(y)) #Sat Nov 10 18:41:24 2018 print(y.tm_year,y.tm_yday) #2018 314 z = datetime.datetime.now() print(z) #2018-11-10 18:51:47.737317 w = z +datetime.timedelta(hours = 10) print('***',w) #*** 2018-11-11 04:51:47.737317 rez = z.replace(minute=20) print(rez) # 2018-11-10 18:20:47.737317 print(datetime.datetime.fromtimestamp(time.time())) #2018-11-10 18:51:47.737317 print(time.time())
3 random模块
3.1 random练习操作系统
__author__ = 'admin' import random print(random.random()) ## float between 0 -1 print(random.uniform(1,9)) # float between 1 -9 print(random.randint(2,10)) #inter from 2 -10 print(random.randrange(2,10)) # not include the last element print(random.choice('hello')) # choice one element print(random.sample('hello',2)) # pick x elements lz =[2,4,6,8,9,10,15] random.shuffle(lz) # disorder the order print(lz)
3.2 随机验证码
__author__ = 'admin' import random ''' lz ='32455656fsafhghfdhr7896' checkcode = '' listc = random.sample(lz,4) print(listc) for i in range(len(listc)): checkcode += listc[i] print(checkcode) ''' checkcode = '' for i in range(5): current = random.randint(0,5) if current == i : temp = chr(random.randint(65,90)) else: temp = random.randint(0,9) checkcode += str(temp) print(checkcode)
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 输出用于分割文件路径的字符串
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所指向的文件或者目录的最后修改时间
5 sys模块
sys.argv 命令行参数List,第一个元素是程序自己路径
sys.exit(n) 退出程序,正常退出时exit(0)
sys.version 获取Python解释程序的版本信息
sys.maxint 最大的Int值
sys.path 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
sys.platform 返回操做系统平台名称
sys.stdout.write('please:')
val = sys.stdin.readline()[:-1]
6 shutil 文件复制
shutil练习:
import shutil,os # f1 = open('TEXTpy2',encoding='utf-8') # f2 = open('testpy3','w',encoding='utf-8') # shutil.copyfileobj(f1,f2) # opened f1 and f2 #shutil.copyfile('testpy3','testpy4') #new testpy4 #shutil.copymode('testpy3','test5') #existed test5 #shutil.copystat('testpy3','test5') #shutil.copytree('module_test','module22') #shutil.rmtree('module22') shutil.make_archive('module_test','gztar') #os.remove('module_test.tar.gz')