1、模块和包a.定义:模块用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py 结尾的python文件。
(例如:文件名:test.py,对应的模块名:test)包:用来从逻辑上组织模块的,本质就是一个目录(必须带有一个__init__.py的文件)b.导入方法import module_nameimport module_1的本质:是将module_1解释了一遍也就是将module_1中的所有代码复制给了module_1from module_name1 import name本质是将module_name1中的name变量放到当前程序中运行一遍所以调用的时候直接print(name)就可以打印出name变量的值代码例子:自己写的模块,其他程序调用,如下所示:模块module_1.py代码:复制代码1 name = "dean"2 def say_hello():3 print("hello %s" %name)调用模块的python程序main代码如下:(切记调用模块的时候只需要import模块名不需要加.py)import module_1#调用变量print(module_)#调用模块中的方法module_1.say_hello()复制代码这样运行main程序后的结果如下:1 D:\python35\python.exe D:/python培训/s14/day5/module_test/main.py2 dean3 hello dean45 Process finished with exit code 0import module_name1,module_name2from module_name import *(这种方法不建议使用)from module_name import logger as log(别名的方法)c.导入模块的本质就是把python文件解释一遍import module_name---->module_name.py---->module_name.py的路径---->sys.path导入包的本质就是执行该包下面的__init__.py关于导入包的一个代码例子:新建一个package_test包,并在该包下面建立一个test1.py的python程序,在package包的同级目录建立一个p_test.py的程序test1的代码如下:1 def test():2 print("int the test1")package_test包下的__init__.py的代码如下:1 #import test1 (理论上这样就可以但是在pycharm下测试必须用下面from .import test1)2 from . import test13 print("in the init")p_test的代码如下:1 import package_test #执行__init__.py2 package_test.test1.test()这样运行p_test的结果:1 D:\python35\python.exe D:/python培训/s14/day5/p_test.py2 in the init3 int the test145 Process finished with exit code 0从上述的例子中也可以看出:导入包的时候其实是执行包下的__init__.py程序,所以如果想要调用包下面的python程序需要在包下的__init__.py导入包下面的程序2、模块的分类a.标准库b.开源模块c.自动以模块3、时间模块time与datetimepython中常见的时间表示方法:a. 时间戳时间戳:从1970年1月1日00:00:00到现在为止一共的时间数(单位为秒)>>> time.time()1472016249.393169>>>b. 格式化的时间字符串c. struct_time(元组)相互之间的转换关系如下:1)time.localtime()将时间戳转换为当前时间的元组>>> time.localtime()time.struct_time(tm_year=2016, tm_mon=8, tm_mday=24, tm_hour=13, tm_min=27, tm_sec=55, tm_wday=2, tm_yday=237, tm_isdst=0)>>>2)time.gmtime()将时间戳转换为当前时间utc时间的元组>>> time.gmtime()time.struct_time(tm_year=2016, tm_mon=8, tm_mday=24, tm_hour=5, tm_min=35, tm_sec=43, tm_wday=2, tm_yday=237, tm_isdst=0)>>>3)time.mktime()可以将struct_time转换成时间戳>>> x = time.localtime()>>> xtime.struct_time(tm_year=2016, tm_mon=8, tm_mday=24, tm_hour=13, tm_min=39, tm_sec=42, tm_wday=2, tm_yday=237, tm_isdst=0)>>> time.mktime(x)1472017182.0>>>4)将struct_time装换成格式化的时间字符串>>> xtime.struct_time(tm_year=2016, tm_mon=8, tm_mday=24, tm_hour=13, tm_min=39, tm_sec=42,tm_wday=2, tm_yday=237, tm_isdst=0)>>> time.strftime("%Y-%m-%d %H:%M:%S",x)'2016-08-24 13:39:42'>>>5)可以将格式化的时间字符串转换为struct_time>>> time.strptime("2016-08-24 14:05:32","%Y-%m-%d %H:%M:%S")time.struct_time(tm_year=2016, tm_mon=8, tm_mday=24, tm_hour=14, tm_min=5, tm_sec=32, tm_wday=2, tm_yday=237, tm_isdst=-1)>>>6)将struct_time转换成Wed Aug 24 14:22:47 2016这种格式>>> xtime.struct_time(tm_year=2016, tm_mon=8, tm_mday=24, tm_hour=14, tm_min=22, tm_sec=47, tm_wday=2, tm_yday=237, tm_isdst=0)>>> time.asctime(x)'Wed Aug 24 14:22:47 2016'>>>7)将时间戳装换成Wed Aug 24 14:22:47 2016格式>>> x = time.time()>>> x1472019984.958831>>> time.ctime(x)'Wed Aug 24 14:26:24 2016'>>>复制代码1 %a 本地(locale)简化星期名称2 %A 本地完整星期名称3 %b 本地简化月份名称4 %B 本地完整月份名称5 %c 本地相应的日期和时间表示6 %d 一个月中的第几天(01 - 31)7 %H 一天中的第几个小时(24小时制,00 - 23)8 %I 第几个小时(12小时制,01 - 12)9 %j 一年中的第几天(001 - 366)10 %m 月份(01 - 12)11 %M 分钟数(00 - 59)12 %p 本地am或者pm的相应符13 %S 秒(01 - 61)14 %U 一年中的星期数。
(00 - 53星期天是一个星期的开始。
)第一个星期天之前的所有天数都放在第0周。
15 %w 一个星期中的第几天(0 - 6,0是星期天)16 %W 和%U基本相同,不同的是%W以星期一为一个星期的开始。
17 %x 本地相应日期18 %X 本地相应时间19 %y 去掉世纪的年份(00 - 99)20 %Y 完整的年份21 %Z 时区的名字(如果不存在为空字符)22 %% ‘%’字符复制代码datetime当前时间:datetime.datetime.now()1、随机模块randomrandom.randint(1,3)则可以取出随机1-3random.randrange(1,3)随机从范围内所及random.choice()传递的参数是序列包括字符串列表等>>> random.choice("hello")'l'>>> random.choice("hello")'o'>>> random.choice("hello")'e'>>>>>> random.choice(["我","爱","你"])'我'>>> random.choice(["我","爱","你"])'你'>>> random.choice(["我","爱","你"])'你'>>> random.choice(["我","爱","你"])'爱'>>>random.sample()随机从前面的序列取出两位>>> random.sample("hello",2)['l', 'o']>>> random.sample("hello",2)['h', 'l']>>> random.sample("hello",2)['h', 'o']>>>random的洗牌功能:>>> a=[1,2,3,4,5,6,7,8,9]>>> random.shuffle(a)>>> a[6, 3, 7, 4, 1, 8, 9, 2, 5]>>>生成随机验证码的例子:复制代码1 import string2 import random3 a = "".join(random.sample(string.ascii_lowercase,4))4 print(a)5 b = "".join(random.sample(string.ascii_lowercase+string.digits,5))6 print(b)78 c = "".join(random.sample(string.ascii_uppercase+string.digits+string.ascii_lowercase,4))9 print(c)10 d ="".join(random.sample(string.ascii_letters+string.digits,4))11 print(d)复制代码运行结果如下:复制代码1 D:\python35\python.exe D:/python培训/s14/day5/验证码2.py2 tbdy3 6te4b4 Z2UA5 v8He67 Process finished with exit code 0复制代码5、os模块复制代码1 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径2 os.chdir("dirname") 改变当前脚本工作目录;相当于shell下cd3 os.curdir 返回当前目录: ('.')4 os.pardir 获取当前目录的父目录字符串名:('..')5 os.makedirs('dirname1/dirname2') 可生成多层递归目录6 os.removedirs('dirname1') 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推7 os.mkdir('dirname') 生成单级目录;相当于shell中mkdir dirname8 os.rmdir('dirname') 删除单级空目录,若目录不为空则无法删除,报错;相当于shell 中rmdir dirname9 os.listdir('dirname') 列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印10 os.remove() 删除一个文件11 os.rename("oldname","newname") 重命名文件/目录12 os.stat('path/filename') 获取文件/目录信息13 os.sep 输出操作系统特定的路径分隔符,win下为"\\",Linux下为"/"14 os.linesep 输出当前平台使用的行终止符,win下为"\t\n",Linux下为"\n"15 os.pathsep 输出用于分割文件路径的字符串16 输出字符串指示当前使用平台。