[Python自学] day-5 (模块、time、random、os、sys、shutil、shelve、configparser、hashlib hmac、re)

一、模块

    模块定义:本质就是一个.py结尾的Python文件。用来从逻辑上组织Python代码(变量、函数、类、逻辑)。 模块就是为了实现一个功能。
#file = modules.py
name = "Leo"
def say_hello():
  print("Hello")
#file = main.py #调用modules模块 import modules #导入模块 print(modules.name) #使用 模块名.变量名 来引用模块中的变量 modules.say_hello() #使用 模块名.函数名 来调用模块中的函数
    导入多个模块:
import module1,module2
 
    模块分类:
  • 标准模块(标准库)
  • 开源模块(第三方模块)
  • 自定义模块
    
    import 和 from ... import ...区别:import 模块名,是导入指定的模块。调用其函数,需要使用 模块名.函数名()。而 from 模块名 import 函数名,是将模块中的函数定义代码复制进来并解释。所以导入文件中不能存在和被导入函数同名的函数。如果存在同名函数,会默认调用本地的函数。
import modules    #import 模块名
modules.say_hello()    #模块名.函数名()

from modules import say_hello #from 模块名 import 函数名 say_hello() #函数名()直接调用

from modules import say_hello def say_hello(): #本地存在say_hello()   print("local say_hello") say_hello() #调用本地的say_hello

def say_hello():   print("local say_hello") from modules import say_hello #复制了modules里的say_hello覆盖本地的say_hello函数变量 say_hello() #调用导入的say_hello
    导入全部变量和函数:from module_name import *  ,不建议使用。
        
    定义别名:
import modules as m
m.say_hello()
from modules import say_hello as leo_say_hello    #leo_say_hello别名
leo_say_hello()
     import的本质:本质是将modules.py文件里的所有代码解释一遍,并统一赋值给modules变量,所以通过modules.name来获取name的值,modules.say_hello()来调用函数。
import modules
    包的定义:本质就是一个目录,必须带有一个__init__.py文件。用来从逻辑上组织模块。
    
    导入包:导入包的本质就是解释包下面的__init__.py文件。
import package_test    #相当于导入__init__
    
    导入不在同一目录的模块:
  • 在sys.path的路径中查到模块文件
  • 需要把待导入文件的目录加入到sys.path中
import os
import sys
p = os.path.abspath(__file__)    #获取文件绝对路径
p_p = os.path.dirname(p)    #获取文件目录
p_p_p = os.path.dirname(p_p)    #获取文件再上一级目录
sys.path.append(p_p_p)    #将搜索目录加入sys.path中
    

二、time模块:

    导入:import time
 
    Python中,用以下几种方式表示时间:
  • 时间戳:time.time(),输出一串数据,单位是s,从1970年开始到现在的时间。
  • 格式化时间:按照自己的格式定义时间。'2017-09-27 22:54:30'
  • 元组:共九个元素。分别是年、月、日、时、分、秒、周的第几天(从0开始)、当年第几天、是否夏令时。
print(time.time())    #时间戳 ,输出1506524291.994569
print(time.localtime(1306324267))    #返回元组。用于取值
    UTC:世界协调时,即格林威治天文时间。北京为UTC-8,即比标准时间早8个小时。在子午线的东边。
    获取时区:
print(time.timezone/3600)   #timezone获取的时间单位是s,除3600换算成小时。
    time.time():获取时间戳
    time.localtime():将时间戳转换为元组,无参数默认当前时间戳
time_tuple = time.localtime()    #获取当前时区、当前时间的元组
print(time_tuple.tm_year)    #获取年
    time.mktime():将元组转换为时间戳
time_stamp = time.mktime(time_tuple)
print(time_stamp)    #返回时间戳
    time.gmtime():返回当前时间对应UTC的时间元组
    time.timezone:获取时区
    time.altzone:UTC和夏令时的差值
    time.daylight:是否使用夏令时
    time.sleep():睡眠,单位s
    time.strftime():将元组格式化为字符串
print(time.strftime("%y-%m-%d %X",time.localtime()))
    time.strptime():将格式化的字符串时间转换为元组
print(time.strptime("2017-09-27 23:41:21",'%Y-%m-%d %H:%M:%S'))
        %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.
    
datetime模块:import datetime导入
 
    datetime.datetime.now():获取当前时间
import datetime
now_time = datetime.datetime.now()
print(now_time)    #打印 2017-09-28 00:07:41.636529
    datetime.timedelta():时间间隔。与datetime.datetime.now()配合使用。
print(datetime.datetime.now()+datetime.timedelta(3))    #获取3天后时间
print(datetime.datetime.now()+datetime.timedelta(-3))    #获取3天前时间
print(datetime.datetime.now()+datetime.timedelta(hours=3))    #获取3小时后
print(datetime.datetime.now()+datetime.timedelta(minutes=-20))   #获取20分钟前

三、random模块

    导入:import random
 
    random.random():产生一个0-1的浮点值。
print(random.random())    #输出0.8767524392823185
    random.randint():产生范围内的随机int值。
print(random.randint(1,3))    #返回[1,3]的值,包含1,2,3
    random.randrange():range是顾头不顾尾。
print(random.randrange(3))    #返回[0,3)的值,包含0,1,2
    random.choice():传入一个序列,字符串、列表、元组等(不包含集合和字典,因为不支持序列即index)。然后随机在其中选取一个。
print(random.choice("Hello"))    #返回H、e、l、o其中一个字符
    random.uniform():在random.random()的基础上增加区间功能。
print(random.uniform(1,3))    #返回[1,3]之间的浮点数 2.0503629577638613
    random.simple():从一个序列中,选择任意几个元素组成一个新的序列。
print(random.sample('abcdefghijk',3))    #返回字符串中任意3个字符,组成列表。例如[b,j,k]
    random.shuffle():将一个有序的列表,洗牌。参数只能是list
item = [1,2,3,4,5,6,7,8]
print(item)    #输出[1,2,3,4,5,6,7,8]
random.shuffle(item)
print(item)    #输出[3,5,4,1,6,8,7,2]
 

四、os模块

    导入:import os
 
    作用:os模块提供对操作系统进行调用的接口。
    
    os.getcwd():获取当前目录
print(os.getcwd())    #返回 D:pycharm_workspaceFirstProjectday6
    os.chdir():切换当前目录
os.chdir("D:\pycharm_workspace")
os.chdir(r"D:pycharm_workspace")    #建议使用这种
print(os.getcwd())
    os.curdir:属性,当前目录 ' . '
    os.purdit:属性,父目录 ' .. '
    os.makedirs():递归的创建多层目录
os.makedirs(r"D:Leoleoeoo")
os.makedirs(r"D:Leoleoeoo",mode=0o777,exist_ok=True)    #exist_ok为True,目录存在时不报错。
    os.removedirs():递归的删除多层目录(内容为空的目录)
os.removedirs(r"D:Leoleoeoo")    #当o为空,则删除eo,eo为空则继续删除leo,一直递归删除到D:
    os.mkdir():创建单层目录
    os.rmdir():删除单层目录(内容为空的目录)
    os.listdir():列出某个目录下的所有文件个子目录,包含隐藏文件,以列表形式打印。    
print(os.listdir("D:\"))
    os.remove():删除一个文件
    os.rename():修改文件或目录名称
os.rename("D:\Leo","D:\Oel")
    os.stat():返回文件信息。其中包括重要的最近访问时间、最近修改时间、创建时间。
print(os.stat("D:\Oel"))
    os.sep:属性,返回特定操作系统下的目录分隔符。windows是\,linux是/
print(os.stat("D:"+os.sep+"Oel"+os.sep+"hello.txt"))
    os.linesep:属性,返回当前操作系统下的换行符。windows下是 ,linux下是
    os.pathsep:属性,返回操作系统下用于分割文件路径的字符。例如path下的文件路径分割。Windows下是;linux下是 :
 
    os.name:属性,返回当前使用平台,windows是nt,linux是posix
    os.environ:属性,返回操作系统环境变量。字典。
print(os.environ["PATH"])    #获取PATH环境变量
    os.system():执行操作系统命令。
os.system("dir")
os.system("ipconfig")
    os.path.abspath():获取文件的绝对路径。
    
    os.path.split():分割,分割路径,返回一个两个元素的元组。
print(os.path.split("D:\Oel\eo\hello.txt"))    #('D:\Oel\eo', 'hello.txt')
    os.path.dirname():返回上一级目录。
 
    os.path.basename():返回os.path.split的第1个元素。
print(os.path.basename("D:\Oel\eo\hello.txt"))    #返回 hello.txt
    os.path.exists():返回目录是否存在,True或False
print(os.path.exists("D:\Oel"))
    os.path.isabs():是否是绝对路径
print(os.path.isabs("D:\Oel"))    #返回True(windwos下)
print(os.path.isabs("Oel"))    #返回False
print(os.path.isabs("/Oel"))    #返回True(linux下)
    os.path.isfile():判断是否是文件
print(os.path.isfile("D:\Oel\hello.txt"))    #返回True
    os.path.isdir():判断是否是目录
print(os.path.isdir("D:\Oel\hello.txt"))    #返回False
    os.path.join():组合几个路径。
print(os.path.join("D:\","Oel\","hello.txt"))    #返回 D:Oelhello.txt
    os.path.getatime():返回指定路径文件或者目录的最后访问时间戳。
    os.path.getmtime(): 返回指定路径文件或者目录的最后修改时间戳。
    os.path.getctime():返回指定路径文件或者目录的创建时间戳。
 
五、sys模块
    导入:import sys
 
    sys.argv:返回命令行参数列表,第一个元素是程序本身路径(相对路径)
    sys.exet(n):退出程序,正常退出时exit(0)
    sys.version:获取Python解释器的版本信息
    sys.path:返回模块搜索路径。
    sys.platform:发挥操作系统平台名称。windows为win32
    sys.stdout.write():输出内容到标准输出(命令行)
    sys.stdin.readline():从标准输入接受一行
input = sys.stdin.readline()
print(input)
    

六、shutil模块

    导入:import shutil
    
    高级的文件、文件夹、压缩包处理模块。
 
    shutil.copyfileobj():拷贝文件,参数为文件句柄。
import shutil
f1 = open("fsrc.txt","r",encoding="utf-8")
f2 = open("fdst.txt","w",encoding="utf-8")
shutil.copyfileobj(f1,f2)
    shutil.copyfile():参数为源文件和目标文件的名称。实现原理是打开文件,并调用copyfileobj()。
import shutil
shutil.copyfile("fsrc.txt","fdst.txt")
    shutil.copymode():拷贝权限。
    shutil.copystat():拷贝文件状态。
    shutil.copy():拷贝文件和权限。
import shutil
shutil.copy("fsrc.txt","D:\Oel\fdst.txt")    #将fsrc.txt文件拷贝到D:Oelfdst.txt
shutil.copy("fsrc.txt","D:\Oel\")    #将fsrc.txt文件拷贝到D:Oel目录下
    shutil.copy2():拷贝文件、状态(包含权限)。
    shutil.copytree():递归的拷贝文件,拷贝目录。
import shutil
shutil.copytree("a","a1")    # a文件夹与执行文件处于一层
    shutil.rmtree():删除目录。
import shutil
shutil.rmtree("a")
    shutil.move():移动文件
    
    shutil.make_archive():打包,压缩。
    参数:
        base_name  压缩包的文件名,也可以是路径,若只写文件名,默认当前目录。
        format 压缩包种类:zip,tar,bztar,gztar
        root_dir 要压缩的文件路径(默认为当前目录)
        owner 用户,默认当前用户
        group 组,默认当前组
        logger 用于记录日志,通常是logging.Logger对象
 
    shutil.make_archive()实际上是调用的ZipFileTarFile模块来进行压缩和解压缩的。
    我们可以直接使用 import ZipFile 或 TarFile来压缩文件。并且可以根据自己需求选择压缩的文件。
import zipfile
z = zipfile.ZipFile("test.zip","w")
z.write("__init__.py")
z.write("makezip.py")
z.close()
 

七、shelve模块

    shelve模块可以提供pickle模块dumps和loads多次的效果。
 
    shalve可以序列化各种数据类型,但仅限于Python。
 
序列化:
import shelve
d = shelve.open("shelve_test")  #打开一个文件
class Test(object):    #定义类
  def __init__(self,n):
    self.n = n
  def pn(self):
    print(self.n)
t1 = Test(123)    #定义对象t1
t2 = Test(234)    #定义对象t2
name = ["alex","rain","test"]    #列表
age = {"alex":1,"rain":2,"test":3}    #字典
job = ("IT","金融","房地产")    #元组
addr = {"成都","海口","南京"}    #集合
d["name"] = name    #将各数据类型序列化,通过字典的方式存储在文件shelve_test中
d["age"] = age
d["job"] = job
d["addr"] = addr
d["t1"] = t1
d["t2"] = t2
d.close()    #关闭序列化文件
反序列化:
import shelve
class Test(object):
  def __init__(self,n):
    self.n = n
  def pn(self):
    print(self.n)
d = shelve.open("shelve_test")    #读模式打开序列化文件
print(d.get("name"))    #使用get()读取数据
print(d.get("age"))
print(d.get("job"))
print(d.get("addr"))
tt1 = d.get("t1")   #读取函数或对象,必须存在对应的类定义和函数定义
tt2 = d.get("t2")
tt1.pn()
tt2.pn()
d.close()

八、xml模块

    参考视频 (五-10-9m)
 
 

九、ConfigParser模块

    处理类似OpenStack组件配置文件。
    类似这种:
[Default]
Name = Leo
Version = 2.1.0
User = leo

[Addrees]
IP = 192.168.1.1
Mac = 22:22:22:22:22:22
    如何写一个配置文件:
import configparser    #导入模块
config = configparser.ConfigParser()    #产生一个实例
config["Default"] = {"Name":"Leo","Age":22,"Addr":"成都"}    
config["Info"] = {}

with open('example.ini','w',encoding="utf-8") as config_file:    #打开配置文件
  config.write(config_file)    #写入配置
    写入结果:
[Default]
name = Leo
age = 22
addr = 成都

[Info]
    如何读取一个配置文件:
import configparser
config = configparser.ConfigParser()
config.read('example.ini')    #直接读配置
print(config.sections())    #打印所有section名称,返回列表。例如["Default","Info"]
print(config["Default"]["Name"])    #打印配置数据
    删除其中一个Section:
import configparser
config = configparser.ConfigParser()
config.read('example.ini')
config.remove_section("Info")
with open('example.ini','w') as f:
  config.write(f)

十、hashlib模块,hmac模块

    用于加密操作,3.x里替代了md5模块和sha模块,主要提供SHA1,SHA224,SHA256,SHA384,SHA512,MD5算法。SHA越后面越安全。
 
    网站防串改:定期wget自己的网页,然后使用MD5加密以下,对比两次值是否一样,就知道你的网页是否被串改了。
 
    MD5加密用法:
import hashlib    #导入模块
m = hashlib.md5()    #获取MD5加密对象
m.update(b"Hello")    #加密Hello
print(m.hexdigest())    #解密成16进制格式。输出8b1a9953c4611296a827abf8c47804d7
m.update(b"World")    #加密Hello+World
print(m.hexdigest())    
m2 = hashlib.md5()    #输出68e109f0f40ca72a15e05cc22786f8e6
m2.update(b"HelloWorld")    #加密HelloWorld,对比两次是否一样
print(m.hexdigest())    #输出68e109f0f40ca72a15e05cc22786f8e6,与前面update后加密值一样
    SHA512使用方法(同MD5):
import hashlib
m = hashlib.sha512()
m.update(b"Hello")
print(m.hexdigest())
m.update(b"World")
print(m.hexdigest())
m2 = hashlib.sha512()
m2.update(b"HelloWorld")
print(m.hexdigest())    #返回 8ae6ae71a75d3fb2e0225deeb004faf95d816a0a58093eb4cb5a3aa0f197050d7a4dc0a2d5c6fbae5fb5b0d536a0a9e6b686369fa57a027687c3630321547596
   
 hmac模块:
    
    先将数据创建成key-value内容。然后在加密,双重保险。
import hmac
h = hmac.new(b"Leo")    #创建key
h.update(b"helloworld")    #加入内容
print(h.hexdigest())
    
    前面所有的update()方法中,如果出现中文,需要转换为utf-8编码。
m.update("你好,这里是中国移动".encode(encoding="utf-8"))
import hashlib
md1 = hashlib.md5()
md1.update("hello".encode(encoding="utf-8"))
md2 = hashlib.md5()
md2.update(b"hello")
print(md1.hexdigest())    #两种方式出来结果一致
print(md2.hexdigest())

十一、re正则表达式模块

    用于动态模糊的匹配字符串。
import re
res = re.match("^leod+","leo123")    #第一个参数是匹配模式,第二个是字符串
if res:    #如果没值就是None
  print(res.group())    #匹配到了就使用group获取
    ' . ':默认匹配 之外的任意一个字符,若指定flag DOTALL,则匹配任意字符,包含换行符。
    '^':匹配开头
    '$':匹配结尾
    '*':匹配*前面的字符 0次或多次
    '+':匹配+前面的字符 一次或多次
    '?':匹配?前的字符 一次或0次
    '{m}':匹配前一个字符 m次
    '{n,m}':匹配前一个字符 n-m次
    '|':匹配 | 左或右的字符
    '(...)':分组
    '[]':范围,例如[A-z][0-6]
 
    'd':匹配数字0-9
    'D':匹配非数字
    'w':匹配[A-Za-z0-9]
    'W':匹配非[A-Za-z0-9]
    's':匹配空白字符,例如 , , 。
 
    re.match():从头开始匹配整个字符串,若匹配,则返回匹配的值,若匹配不到,则返回None
import re
res = re.match('d{11}',"number 18111566225" )    
if res:
  print(res.group())    #匹配不到
import re res = re.match('[0-9]{11}',"18111566222" ) if res:   print(res.group()) #匹配到18111566222

import re res = re.match('[1-9]{11}$',"18111566222abc" ) if res:   print(res.group()) #匹配不到,因为$代表结尾
  re.search():在字符串中查到可以匹配的部分,并返回第一个匹配到的值。
import re
res = re.search('numw+sw+sd{11}',"Telephone number is 18111566222" )
print(res.group())    #匹配到number is 18111566222
    re.findall():在字符串中匹配所有可以匹配到的值,并返回一个列表。
import re
res = re.findall('numw+sw+sd{11}',"Telephone number is 18111566222,number is 18111566222" )
print(res)    #返回列表["number is 18111566222","number is 18111566222"]
保持学习,否则迟早要被淘汰*(^ 。 ^ )***
原文地址:https://www.cnblogs.com/leokale-zz/p/8472530.html