Python高手之路【七】python基础之模块

1,276次阅读
没有评论

本节大纲


  1. 模块介绍
  2. time &datetime模块
  3. random
  4. os
  5. sys
  6. shutil
  7. json & picle
  8. shelve
  9. xml处理
  10. yaml处理
  11. configparser
  12. hashlib
  13. subprocess
  14. logging模块
  15. re正则表达式

1:模块介绍

模块,用一砣代码实现了某个功能的代码集合。

类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合。而对于一个复杂的功能来,可能需要多个函数才能完成(函数又可以在不同的.py文件中),n个 .py 文件组成的代码集合就称为模块。

如:os 是系统相关的模块;file是文件操作相关的模块

模块分为三种:

  • 自定义模块
  • 内置标准模块(又称标准库)
  • 开源模块

模块导入的三种方式:

方法一:

  1. import modname

用import语句导入模块,就在当前的名称空间(namespace)建立了一个到该模块的引用.这种引用必须使用全称,也就是说,当使用在被导入模块中定义的函数时,必须包含模块的名字。所以不能只使用 funcname,而应该使用 modname.funcname

方法二:

  1. from modname import funcname
  2. from modname import fa, fb, fc
  3. from modname import *

与第1种方法的区别:funcname 被直接导入到本地名字空间去了,所以它可以直接使用,而不需要加上模块名的限定* 表示,该模块的所有公共对象(public objects)都被导入到 当前的名称空间,也就是任何只要不是以”_”开始的东西都会被导入。 modname没有被定义,所以modname.funcname这种方式不起作用。并且,如果funcname如果已经被定义,它会被新版本(该导入模块中的版本)所替代。如果funcname被改成指向其他对象,modname不能不会觉察到。 建议:

  1. 如果你要经常访问模块的属性和方法,且不想一遍又一遍地敲入模块名,使用 from module import
  2. 如果你想要有选择地导入某些属性和方法,而不想要其它的,使用 from module import
  3. 如果模块包含的属性和方法与你的某个模块同名,你必须使用import module来避免名字冲突
  4. 尽量少用 from module import * ,因为判定一个特殊的函数或属性是从哪来的有些困难,并且会造成调试和重构都更困难。

2:time & datetime模块

  1. #_*_coding:utf-8_*_
  2. __author__ = ‘Alex Li’
  3.  
  4. import time
  5.  
  6. # print(time.clock()) #返回处理器时间,3.3开始已废弃 , 改成了time.process_time()测量处理器运算时间,不包括sleep时间,不稳定,mac上测不出来
  7. # print(time.altzone) #返回与utc时间的时间差,以秒计算\
  8. # print(time.asctime()) #返回时间格式”Fri Aug 19 11:14:16 2016″,
  9. # print(time.localtime()) #返回本地时间 的struct time对象格式
  10. # print(time.gmtime(time.time()-800000)) #返回utc时间的struc时间对象格式
  11.  
  12. # print(time.asctime(time.localtime())) #返回时间格式”Fri Aug 19 11:14:16 2016″,
  13. #print(time.ctime()) #返回Fri Aug 19 12:38:29 2016 格式, 同上
  14.  
  15. # 日期字符串 转成 时间戳
  16. # string_2_struct = time.strptime(“2016/05/22″,”%Y/%m/%d”) #将 日期字符串 转成 struct时间对象格式
  17. # print(string_2_struct)
  18. # #
  19. # struct_2_stamp = time.mktime(string_2_struct) #将struct时间对象转成时间戳
  20. # print(struct_2_stamp)
  21.  
  22. #将时间戳转为字符串格式
  23. # print(time.gmtime(time.time()-86640)) #将utc时间戳转换成struct_time格式
  24. # print(time.strftime(“%Y-%m-%d %H:%M:%S”,time.gmtime()) ) #将utc struct_time格式转成指定的字符串格式
  25.  
  26. #时间加减
  27. import datetime
  28.  
  29. # print(datetime.datetime.now()) #返回 2016-08-19 12:47:03.941925
  30. #print(datetime.date.fromtimestamp(time.time()) ) # 时间戳直接转成日期格式 2016-08-19
  31. # print(datetime.datetime.now() )
  32. # print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天
  33. # print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天
  34. # print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时
  35. # print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分
  36.  
  37. #
  38. # c_time = datetime.datetime.now()
  39. # print(c_time.replace(minute=3,hour=2)) #时间替换

3:random

  1. import random
  2.  
  3. print(random.random())
  4. print(random.randint(1, 2))
  5. print(random.randrange(1, 10))

4:OS模块

用于提供系统级别的操作:

  1. os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径
  2. os.chdir(“dirname”) 改变当前脚本工作目录;相当于shellcd
  3. os.curdir 返回当前目录: (‘.’)
  4. os.pardir 获取当前目录的父目录字符串名:(‘..’)
  5. os.makedirs(‘dir1/dir2’) 可生成多层递归目录
  6. os.removedirs(‘dirname1’) 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
  7. os.mkdir(‘dirname’) 生成单级目录;相当于shellmkdir dirname
  8. os.rmdir(‘dirname’) 删除单级空目录,若目录不为空则无法删除,报错;相当于shellrmdir dirname
  9. os.listdir(‘dirname’) 列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
  10. os.remove() 删除一个文件
  11. os.rename(“oldname”,“new”) 重命名文件/目录
  12. os.stat(‘path/filename’) 获取文件/目录信息
  13. os.sep 操作系统特定的路径分隔符,win下为“\\”,Linux下为“/”
  14. os.linesep 当前平台使用的行终止符,win下为“\t\n”,Linux下为“\n”
  15. os.pathsep 用于分割文件路径的字符串
  16. os.name 字符串指示当前使用平台。win->‘nt’; Linux->‘posix’
  17. os.system(“bash command”) 运行shell命令,直接显示
  18. os.environ 获取系统环境变量
  19. os.path.abspath(path) 返回path规范化的绝对路径
  20. os.path.split(path) path分割成目录和文件名二元组返回
  21. os.path.dirname(path) 返回path的目录。其实就是os.path.split(path)的第一个元素
  22. os.path.basename(path) 返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素
  23. os.path.exists(path) 如果path存在,返回True;如果path不存在,返回False
  24. os.path.isabs(path) 如果path是绝对路径,返回True
  25. os.path.isfile(path) 如果path是一个存在的文件,返回True。否则返回False
  26. os.path.isdir(path) 如果path是一个存在的目录,则返回True。否则返回False
  27. os.path.join(path1[, path2[, …]]) 将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
  28. os.path.getatime(path) 返回path所指向的文件或者目录的最后存取时间
  29. os.path.getmtime(path) 返回path所指向的文件或者目录的最后修改时间

5:sys模块

sys模块用于提供对python解释器相关的操作:

  1. sys.argv 命令行参数List,第一个元素是程序本身路径
  2. sys.exit(n) 退出程序,正常退出时exit(0)
  3. sys.version 获取Python解释程序的版本信息
  4. sys.maxint 最大的Int
  5. sys.path 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
  6. sys.platform 返回操作系统平台名称
  7. sys.stdout.write(‘please:’)
  8. val = sys.stdin.readline()[:-1]

练习题:

1:读写用户的输入,根据用户输入,创建一个相应的目录

  1. import sys,os
  2. print(sys.argv)
  3. os.mkdir(sys.argv[1])

在命令行下执行:

  1. D:\Python\Python3532>python e:/wwwroot/py
  2. [‘e:/wwwroot/python/index.py’, ‘testdir’]

执行完毕,在D:\Python\Python35-32目录下就多了一个testdir的目录

2:自己写一个简单的脚本,可以在任何路径导入

3:进度条,带百分比

  1. import sys,time
  2. for i in range(31):
  3. sys.stdout.write(‘\r’)
  4. sys.stdout.write(“%s%% |%s” % (int(i/30*100),int(i/30*100)*‘*’))
  5. sys.stdout.flush()
  6. time.sleep(0.1)

如何添加sys.path路径:

  1. import sys
  2. sys.path.append(“D:”)#这里写需要添加的路径
  3. for i in sys.path:
  4. print(i)

6:shuntil

高级的 文件、文件夹、压缩包 处理模块

shutil.copyfileobj(fsrc, fdst[, length]):将文件内容拷贝到另一个文件中

  1. import shutil
  2.  
  3. shutil.copyfileobj(open(‘old.xml’,‘r’), open(‘new.xml’, ‘w’))

shutil.copyfile(src, dst):拷贝文件

  1. shutil.copyfile(‘f1.log’, ‘f2.log’)

shutil.copymode(src, dst):仅拷贝权限。内容、组、用户均不变

  1. shutil.copymode(‘f1.log’, ‘f2.log’)

shutil.copystat(src, dst):仅拷贝状态的信息,包括:mode bits, atime, mtime, flags

  1. shutil.copystat(‘f1.log’, ‘f2.log’)

shutil.copy(src, dst):拷贝文件和权限

  1. import shutil
  2.  
  3. shutil.copy(‘f1.log’, ‘f2.log’)

shutil.copy2(src, dst):拷贝文件和状态信息

  1. import shutil
  2.  
  3. shutil.copy2(‘f1.log’, ‘f2.log’)

shutil.ignore_patterns(*patterns) shutil.copytree(src, dst, symlinks=False, ignore=None) 递归的去拷贝文件夹

  1. import shutil
  2.  
  3. shutil.copytree(‘folder1’, ‘folder2’, ignore=shutil.ignore_patterns(‘*.pyc’, ‘tmp*’))
  1. import shutil
  2.  
  3. shutil.copytree(‘f1’, ‘f2’, symlinks=True, ignore=shutil.ignore_patterns(‘*.pyc’, ‘tmp*’))

shutil.rmtree(path[, ignore_errors[, onerror]]) 递归的去删除文件

  1. import shutil
  2.  
  3. shutil.rmtree(‘folder1’)

shutil.move(src, dst) 递归的去移动文件,它类似mv命令,其实就是重命名。

  1. import shutil
  2.  
  3. shutil.move(‘folder1’, ‘folder3’)

shutil.make_archive(base_name, format,…)

创建压缩包并返回文件路径,例如:zip、tar

创建压缩包并返回文件路径,例如:zip、tar

  • base_name: 压缩包的文件名,也可以是压缩包的路径。只是文件名时,则保存至当前目录,否则保存至指定路径,
  • 如:www =>保存至当前路径
  • 如:/Users/wupeiqi/www =>保存至/Users/wupeiqi/
  • format: 压缩包种类,“zip”, “tar”, “bztar”,“gztar”
  • root_dir: 要压缩的文件夹路径(默认当前目录)
  • owner: 用户,默认当前用户
  • group: 组,默认当前组
  • logger: 用于记录日志,通常是logging.Logger对象
  1. #将 /Users/wupeiqi/Downloads/test 下的文件打包放置当前程序目录
  2. import shutil
  3. ret = shutil.make_archive(“wwwwwwwwww”, ‘gztar’, root_dir=‘/Users/wupeiqi/Downloads/test’)
  4.  
  5. #将 /Users/wupeiqi/Downloads/test 下的文件打包放置 /Users/wupeiqi/目录
  6. import shutil
  7. ret = shutil.make_archive(“/Users/wupeiqi/wwwwwwwwww”, ‘gztar’, root_dir=‘/Users/wupeiqi/Downloads/test’)

shutil 对压缩包的处理是调用 ZipFile 和 TarFile 两个模块来进行的,详细:

  1. import zipfile
  2.  
  3. # 压缩
  4. z = zipfile.ZipFile(‘laxi.zip’, ‘w’)
  5. z.write(‘a.log’)
  6. z.write(‘data.data’)
  7. z.close()
  8.  
  9. # 解压
  10. z = zipfile.ZipFile(‘laxi.zip’, ‘r’)
  11. z.extractall()
  12. z.close()
  13.  
  14. zipfile解压缩

ZIPfile解压缩

  1. import tarfile
  2.  
  3. # 压缩
  4. tar = tarfile.open(‘your.tar’,‘w’)
  5. tar.add(‘/Users/wupeiqi/PycharmProjects/bbs2.log’, arcname=‘bbs2.log’)
  6. tar.add(‘/Users/wupeiqi/PycharmProjects/cmdb.log’, arcname=‘cmdb.log’)
  7. tar.close()
  8.  
  9. # 解压
  10. tar = tarfile.open(‘your.tar’,‘r’)
  11. tar.extractall() # 可设置解压地址
  12. tar.close()
  13.  
  14. tarfile解压缩

tarfile解压缩

7:json & pickle 模块

用于序列化的两个模块

  • json,用于字符串 和 python数据类型间进行转换
  • pickle,用于python特有的类型 和 python的数据类型间进行转换

Json模块提供了四个功能:dumps、dump、loads、load

pickle模块提供了四个功能:dumps、dump、loads、load

pickle模块的dumps,loads演示:

假如现在有一个字典弄的数据,内容如下:

  1. accounts = {
  2. 1000:{
  3. ‘name’:‘poe’,
  4. ‘password’:,
  5. ’email’:‘ginvip@qq.com’,
  6. ‘balance’:15000,
  7. ‘phone’:13888888888,
  8. ‘back_acc’:{‘ICBC’:62202,‘CBC’:52202,‘ABC’:42202}
  9. },
  10. 1001:{
  11. ‘name’:‘jet’,
  12. ‘password’:,
  13. ’email’:‘jet@qq.com’,
  14. ‘balance’:150,
  15. ‘phone’:13888888888,
  16. ‘back_acc’:{‘ICBC’:62202}
  17. }
  18. }

那么如何将这个字典型的数据持久化保存到硬盘里呢?由于文件的写入只允许保存字符串,所以一般的方法无法将该字典型数据保存到硬盘里,这里就需要用到pickle模块的dumps与loads功能

  1. import pickle
  2. accounts = {
  3. 1000:{
  4. ‘name’:‘poe’,
  5. ‘password’:,
  6. ’email’:‘ginvip@qq.com’,
  7. ‘balance’:15000,
  8. ‘phone’:13888888888,
  9. ‘back_acc’:{‘ICBC’:62202,‘CBC’:52202,‘ABC’:42202}
  10. },
  11. 1001:{
  12. ‘name’:‘jet’,
  13. ‘password’:,
  14. ’email’:‘jet@qq.com’,
  15. ‘balance’:150,
  16. ‘phone’:13888888888,
  17. ‘back_acc’:{‘ICBC’:62202}
  18. }
  19. }
  20. f = open(‘account.db’,‘wb’)
  21. f.write(pickle.dumps(accounts))
  22. f.close()

account.py

数据保存后,那么如果需要重写,该怎么办呢?

  1. import pickle
  2. acc_file_name = “account.db”
  3. account_file = open(acc_file_name,‘rb’)
  4. # print(pickle.loads(account_file.read()))
  5. account_dic = pickle.loads(account_file.read())
  6. account_file.close()
  7. account_dic[1000][‘balance’] -= 500
  8. f = open(acc_file_name,‘wb’)
  9. f.write(pickle.dumps(account_dic))
  10. f.close()
  11. print(account_dic)

shopping.py

验证数据是否写入成功:

  1. import pickle
  2. f = open(“account.db”,‘rb’)
  3.  
  4. account_db = pickle.loads(f.read())
  5. print(account_db)

check.py

  1. import json
  2. s = ‘{“key1″:”value1″,”key2″:”value2”}’ # ==> 用json模块将字符串转化成其他数据类型,字符串里出现引号必须用双引号
  3. ret = json.loads(s) # ==> loads 由字符串转其他数据类型
  4. print(ret,type(ret))
  5.  
  6. ret = json.load(open(‘ethan.txt’,‘r’)) # ==> 将文档(内部是字符串格式)转换成python的其他数据类型
  7. print(ret,type(ret)) # ==> 文档里是字典样式的字符串
  8.  
  9. l = ‘[11,22,3,56,75]’
  10. result =json.loads(l)
  11. print(result,type(result))
  12. # 总结:
  13. # json.loads()用于将形似字典、列表、元组的字符串,转换成字典、列表、元组
  14. # json.load() 用于将文档(内容是形似字典、列表、元组的字符串)转换成字典、列表、元组
  15.  
  16. di = {“key1”:“value1”,“key2”:“value2”}
  17. ret = json.dumps(di) # ==> 将字典、列表、元组 转换成字符串格式
  18. print(ret,type(ret))
  19.  
  20. json.dump(di,open(‘ethan.txt’,‘a+’)) # ==> 将字典、元组、列表转换成字符串格式并写入文档
  21.  
  22. import pickle
  23.  
  24. d = {‘name’:‘ethan’,‘age’:28}
  25. ret = pickle.dumps(d) # ==> pickle将字典、元组、列表转换成二进制
  26. print(ret,type(ret))
  27.  
  28. l = [11,22,3,45,54]
  29. res = pickle.dumps(l)
  30. print(res)
  31.  
  32. pickle.dump(d,open(‘ethan.txt’,‘ab’)) # ==> 将字典、元组、列表转换成二进制写入文档
  33.  
  34. # 注意 dump load 不要一起运行,会报错,一步一步来
  35.  
  36. f = open(‘ethan.txt’,‘rb’)
  37. r = pickle.loads(f.read()) # ==> 将二进制转换成字典、列表、元组
  38. print(r)

11:configpaser模块

configparser用于处理特定格式的文件,其本质上是利用open来操作文件。

  1. # 注释1
  2. ; 注释2
  3.  
  4. [section1] # 节点
  5. k1 = v1 # 值
  6. k2:v2 # 值
  7.  
  8. [section2] # 节点
  9. k1 = v1 # 值
  10.  
  11. 指定格式

指定格式

举例:假如现在有一个ini的文件,内容如下:

  1. [poe]
  2. age = 12
  3. gender = ‘female’
  4. edu = ‘daxue’
  5. [jet]
  6. age = 19
  7. gender = ‘male’

1:利用configparser模块提供的功能,获取所有节点

  1. import configparser
  2. con = configparser.ConfigParser()#创建对象
  3. #con对象的read功能,打开文件读取文件,放进内存
  4. con.read(“ini”,encoding=“utf-8”)
  5. #con对象的sections,内在中寻找所有的[xxx]节点
  6. result = con.sections()
  7. print(result)
  8. #con对象的options功能,获取指定[xxx]节点下的内容
  9. ret = con.options(“poe”)
  10. print(ret)
  11. ##########################################
  12. [‘poe’, ‘jet’]
  13. [‘age’, ‘gender’, ‘edu’]

2:获取指定节点下所有的键值对

  1. import configparser
  2. con = configparser.ConfigParser()
  3. con.read(“ini”,encoding=“utf-8”)
  4.  
  5. ret = con.items(‘poe’)
  6. print(ret)
  7. ##########################################
  8. [(‘age’, ), (‘gender’, “‘female'”), (‘edu’, “‘daxue'”)]

3:获取指定节点下指定key的值

  1. import configparser
  2. con = configparser.ConfigParser()
  3. con.read(“ini”,encoding=“utf-8”)
  4.  
  5. ret = con.get(“poe”,“age”)
  6. print(ret)
  7. ##########################################
  8. 12

4:检查、删除、添加节点

  1. import configparser
  2. con = configparser.ConfigParser()
  3. con.read(“ini”,encoding=“utf-8”)
  4.  
  5. # 检查节点是否存在,存在返回True,否则返回False
  6. has_sec = con.has_section(“poe”)
  7. print(has_sec)
  8.  
  9. # 添加节点
  10. con.add_section(“andy”)
  11. con.write(open(“ini”,“w”))
  12.  
  13. # 删除节点
  14. con.remove_section(“trim”)
  15. con.write(open(“ini”,“w”))

5:检查、删除、设置指定组内的键值对

  1. import configparser
  2. con = configparser.ConfigParser()
  3. con.read(“ini”,encoding=“utf-8”)
  4.  
  5. # 检查andy节点下是否存在hobby键
  6. has_sec = con.has_option(“andy”,“hobby”)
  7. print(has_sec)
  8.  
  9. # 删除
  10. con.remove_option(“andy”,“hobby”)
  11. con.write(open(“ini”,“w”))
  12.  
  13. # 设置
  14. con.set(“andy”,“from”,“beijing”)
  15. con.write(open(“ini”,“w”))

总结:

增:

  1. obj.add_section(块节点名称)   增加一个块节点
  2. obj.set(块节点名称,键,值)         在指定的块节点下增加键值

删:

  1. obj.remove_section(块节点名称)             删除指定块节点(连同该块节点下的所有键值都删除)
  2. obj.remove_option(块节点名称,该块节点下的键名)             删除指定块节点下指定键的键值对

改:

  1. obj.set(块节点名称,键,值)

查:

  1. obj.sections()                 查询所有的块节点(且称之为块节点,如:上面文件中的[poe],[jet]),返回一个列表
  2. obj.options(块节点名称)              查询指定块节点下的所有键,返回一个列表
  3. obj.items(块节点名称)                 查询指定块节点下的所有键及值,返回一个列表(每个键值对保存为一个元组)
  4. obj.get(块节点名称,块节点下的键)                      查询指定块节点下,键所对就的值
  5. obj.has_section(块节点名称)                                查询块节点是否存在
  6. obj.has_option(块节点名称,块节点下的键)        查询指定块节点下的键是否存在

12:hashlib

用于加密相关的操作,代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法

  1. import hashlib
  2.  
  3. # ######## md5 ########
  4. hash = hashlib.md5()
  5. # help(hash.update)
  6. hash.update(bytes(‘admin’, encoding=‘utf-8’))
  7. print(hash.hexdigest())
  8. print(hash.digest())
  9.  
  10. ######## sha1 ########
  11.  
  12. hash = hashlib.sha1()
  13. hash.update(bytes(‘admin’, encoding=‘utf-8’))
  14. print(hash.hexdigest())
  15.  
  16. # ######## sha256 ########
  17.  
  18. hash = hashlib.sha256()
  19. hash.update(bytes(‘admin’, encoding=‘utf-8’))
  20. print(hash.hexdigest())
  21.  
  22. # ######## sha384 ########
  23.  
  24. hash = hashlib.sha384()
  25. hash.update(bytes(‘admin’, encoding=‘utf-8’))
  26. print(hash.hexdigest())
  27.  
  28. # ######## sha512 ########
  29.  
  30. hash = hashlib.sha512()
  31. hash.update(bytes(‘admin’, encoding=‘utf-8’))
  32. print(hash.hexdigest())

以上加密算法虽然依然非常厉害,但时候存在缺陷,即:通过撞库可以反解。所以,有必要对加密算法中添加自定义key再来做加密。(推荐使用此方法)

  1. import hashlib
  2.  
  3. # ######## md5 ########
  4.  
  5. hash = hashlib.md5(bytes(‘898oaFs09f’,encoding=“utf-8”))
  6. hash.update(bytes(‘admin’,encoding=“utf-8”))
  7. print(hash.hexdigest())

python内置还有一个 hmac 模块,它内部对我们创建 key 和 内容 进行进一步的处理然后再加密

  1. import hmac
  2.  
  3. h = hmac.new(bytes(‘898oaFs09f’,encoding=“utf-8”))
  4. h.update(bytes(‘admin’,encoding=“utf-8”))
  5. print(h.hexdigest())

基于加密的登录程序:

  1. import hashlib
  2. def md5(arg) :
  3. p = hashlib.md5(bytes(‘admin’,encoding=‘utf-8’))
  4. p.update(bytes(arg,encoding=‘utf-8’))
  5. return p.hexdigest()
  6.  
  7. def register(user,pwd) :
  8. with open(“db”,“a”,encoding=“utf-8”) as f :
  9. temp = user + “|” + md5(pwd)
  10. f.write(temp)
  11. def login(user,pwd):
  12. with open(‘db’,‘r’,encoding=“utf-8”) as f :
  13. for line in f:
  14. u,p = line.strip().split(“|”)
  15. if u == user and p == md5(pwd):
  16. return True
  17. return False
  18. i = input(“1:login 2:register”)
  19. if i == :
  20. user = input(‘username : ‘)
  21. pwd = input(‘password : ‘)
  22. register(user,pwd)
  23. elif i == :
  24. user = input(‘username :’)
  25. pwd = input(‘password :’)
  26. if login(user,pwd) :
  27. print(‘login success’)
  28. else:
  29. print(‘login failed’)

用户登录

13:subprocess模块

执行shell命令

call 

执行命令,返回状态码

  1. ret = subprocess.call([“ls”, “-l”], shell=False)
  2. ret = subprocess.call(“ls -l”, shell=True)

check_call

执行命令,如果执行状态码是 0 ,则返回0,否则抛异常

  1. subprocess.check_call([“ls”, “-l”])
  2. subprocess.check_call(“exit 1”, shell=True)

check_output

执行命令,如果状态码是 0 ,则返回执行结果,否则抛异常

  1. subprocess.check_output([“echo”, “Hello World!”])
  2. subprocess.check_output(“exit 1”, shell=True)

subprocess.Popen(…)

用于执行复杂的系统命令

参数:

  1. args:shell命令,可以是字符串或者序列类型(如:list,元组)
  2. bufsize:指定缓冲。0 无缓冲,1 行缓冲,其他 缓冲区大小,负值 系统缓冲
  3. stdin, stdout, stderr:分别表示程序的标准输入、输出、错误句柄
  4. preexec_fn:只在Unix平台下有效,用于指定一个可执行对象(callable object),它将在子进程运行之前被调用
  5. close_sfs:在windows平台下,如果close_fds被设置为True,则新创建的子进程将不会继承父进程的输入、输出、错误管道。
  6. 所以不能将close_fds设置为True同时重定向子进程的标准输入、输出与错误(stdin, stdout, stderr)。
  7. shell:同上
  8. cwd:用于设置子进程的当前目录
  9. env:用于指定子进程的环境变量。如果env = None,子进程的环境变量将从父进程中继承。
  10. universal_newlines:不同系统的换行符不同,True -> 同意使用 \n
  11. startupinfo与createionflags只在windows下有效
  12. 将被传递给底层的CreateProcess()函数,用于设置子进程的一些属性,如:主窗口的外观,进程的优先级等等
  1. import subprocess
  2. ret1 = subprocess.Popen([“mkdir”,“t1”])
  3. ret2 = subprocess.Popen(“mkdir t2”, shell=True)

终端输入的命令分为两种:

  1. 输入即可得到输出,如:ifconfig
  2. 输入进行某环境,依赖再输入,如:python
  1. import subprocess
  2.  
  3. obj = subprocess.Popen(“mkdir t3”, shell=True, cwd=‘/home/dev’,)
  1. import subprocess
  2.  
  3. obj = subprocess.Popen([“python”], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
  4. obj.stdin.write(“print(1)\n”)
  5. obj.stdin.write(“print(2)”)
  6. obj.stdin.close()
  7.  
  8. cmd_out = obj.stdout.read()
  9. obj.stdout.close()
  10. cmd_error = obj.stderr.read()
  11. obj.stderr.close()
  12.  
  13. print(cmd_out)
  14. print(cmd_error)
  1. import subprocess
  2.  
  3. obj = subprocess.Popen([“python”], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
  4. obj.stdin.write(“print(1)\n”)
  5. obj.stdin.write(“print(2)”)
  6.  
  7. out_error_list = obj.communicate()
  8. print(out_error_list)
  1. import subprocess
  2.  
  3. obj = subprocess.Popen([“python”], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
  4. out_error_list = obj.communicate(‘print(“hello”)’)
  5. print(out_error_list)

14:login模块

用于便捷记录日志且线程安全的模块

1、单文件日志

  1. import logging
  2.  
  3. logging.basicConfig(filename=‘log.log’,
  4. format=‘%(asctime)s – %(name)s – %(levelname)s -%(module)s: %(message)s’,
  5. datefmt=‘%Y-%m-%d %H:%M:%S %p’,
  6. level=10)
  7.  
  8. logging.debug(‘debug’)
  9. logging.info(‘info’)
  10. logging.warning(‘warning’)
  11. logging.error(‘error’)
  12. logging.critical(‘critical’)
  13. logging.log(10,‘log’)

日志等级:

  1. CRITICAL = 50
  2. FATAL = CRITICAL
  3. ERROR = 40
  4. WARNING = 30
  5. WARN = WARNING
  6. INFO = 20
  7. DEBUG = 10
  8. NOTSET = 0

注:只有【当前写等级】大于【日志等级】时,日志文件才被记录。

日志记录格式:

Python高手之路【七】python基础之模块

2、多文件日志

对于上述记录日志的功能,只能将日志记录在单文件中,如果想要设置多个日志文件,logging.basicConfig将无法完成,需要自定义文件和日志操作对象。

  1. # 定义文件
  2. file_1_1 = logging.FileHandler(‘l1_1.log’, ‘a’, encoding=‘utf-8’)
  3. fmt = logging.Formatter(fmt=“%(asctime)s – %(name)s – %(levelname)s -%(module)s: %(message)s”)
  4. file_1_1.setFormatter(fmt)
  5.  
  6. file_1_2 = logging.FileHandler(‘l1_2.log’, ‘a’, encoding=‘utf-8’)
  7. fmt = logging.Formatter()
  8. file_1_2.setFormatter(fmt)
  9.  
  10. # 定义日志
  11. logger1 = logging.Logger(‘s1’, level=logging.ERROR)
  12. logger1.addHandler(file_1_1)
  13. logger1.addHandler(file_1_2)
  14.  
  15. # 写日志
  16. logger1.critical(‘1111’)
  1. # 定义文件
  2. file_2_1 = logging.FileHandler(‘l2_1.log’, ‘a’)
  3. fmt = logging.Formatter()
  4. file_2_1.setFormatter(fmt)
  5.  
  6. # 定义日志
  7. logger2 = logging.Logger(‘s2’, level=logging.INFO)
  8. logger2.addHandler(file_2_1)

如上述创建的两个日志对象

  • 当使用【logger1】写日志时,会将相应的内容写入 l1_1.log 和 l1_2.log 文件中
  • 当使用【logger2】写日志时,会将相应的内容写入 l2_1.log 文件中
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2023-02-16发表,共计15662字。
新手QQ群:570568346,欢迎进群讨论 Python51学习