1.imp模块==========>重新加载已加载过的模块方法

import imp

imp.reload(mymod)  # 重新加载已经加载过的mymod模块

2.ctypes模块=======>加载动态库方法

from ctypes

# 加载由C语言编译的libdead_loop.so动态库文件,并返回一个动态库对象
lib = ctypes.cdll.LoadLibrary("libdead_loop.so")
lib.DeadLoop() # 可以直接执行C语言编译后的动态库中的函数

3.copy模块=========>浅拷贝和深拷贝方法

import copy  # 导入copy模块

li = [3.1, 3.2]
li1 = [1, 2, li] # [1, 2, [3.1, 3.2]]
li2 = copy.copy(li1) # [1, 2, [3.1, 3.2]]浅拷贝
id(li2[2]) == id(li1[2]) # True
li3 = copy.deepcopy(li1) # [1, 2, [3.1, 3.2]]深拷贝
id(li3[2]) == id(li1[2]) # False

4.contextlib模块===>上下文管理装饰器方法

from contextlib import contextmanager

@contextmanager  # 装饰后保证了使用with管理my_open函数执行时产生异常一定会调用close,同理可可以装饰类
def my_open(path, mode):
f = open(path, mode)
# 通过 yield 将函数分割成两部分,yield 之前的语句在 __enter__ 方法中执行
# yield 之后的语句在 __exit__ 方法中执行,紧跟在 yield 后面的值是函数的返回值
yield f
f.close() with my_open('out.txt', 'w') as f:
f.write("hello , the simplest context manager")

5.urllib模块=======>url编解码方法

import urllib.parse

# Python3 url编码
print(urllib.parse.quote("响应头 响应体")) # %E5%93%8D%E5%BA%94%E5%A4%B4%20%E5%93%8D%E5%BA%94%E4%BD%93
# Python3 url解码
print(urllib.parse.unquote("%E5%93%8D%E5%BA%94%E5%A4%B4%20%E5%93%8D%E5%BA%94%E4%BD%93")) # 响应头 响应体

6.traceback模块====>更专业的打印异常信息方法

import traceback

a = 10
try:
b = a / ''
except TypeError:
traceback.print_exc()
print("---end---")
"""执行结果
Traceback (most recent call last):
File "<ipython-input-1-929daf19ab90>", line 5, in <module>
b = a / '2'
TypeError: unsupported operand type(s) for /: 'int' and 'str'
---end---
"""

7.keyword模块======>列表形式列出Python中的所有关键字方法

import keyword

print(keyword.kwlist)  # 返回一个包含Python关键字的列表

8.types模块========>判断一个对象是方法还是函数

from types import FunctionType
from types import MethodType class MyClass: def test(self):
print("方法被调用") def func():
print("函数被调用") my_obj = MyClass() # 判断一个对象是否是函数
print(isinstance(func,FunctionType)) # True
# 类名.方法名是函数
print(isinstance(MyClass.test,FunctionType)) # True
# 判断一个对象是否是函数
print(isinstance(obj.test,FunctionType)) # False
# 实例.方法名是方法
print(isinstance(obj.test,MethodType)) # True

9.getpass模块======>隐式输入密码方法

In [1]: import getpass

In [2]: passwd = getpass.getpass()  # 在输入时会有密码输入提示,且加密输入
Password: In [3]: passwd1 = getpass.getpass("确认密码") # 可以指定提示语
确认密码: In [4]: passwd, passwd1
Out[4]: ('', '')

10.functools模块===>对参数序列中元素进行累积方法

from functools import reduce

num1 = [1, 2, 3, 4, 5]
# reduce方法遍历合并可迭代对象中的每个元素
msg = reduce(lambda i, j: i + j, num1, 10)
print(msg) #

11.subprocess模块==>执行系统命令方法

import subprocess

# 语法: res = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.subprocess.PIPE)
# cmd: 代表系统命令
# shell=True: 代表这条命令是系统命令,告诉操作系统将cmd当做系统命令去执行
# stdout: 是执行系统命令后用于保存正确结果的一个管道
# stderr: 是执行系统命令后用于保存错误结果的一个管道 r = subprocess.Popen("pwd", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print("正确的返回结果是: %s" % r.stdout.read().decode("utf-8")) # 正确的返回结果是: /Users/tangxuecheng r = subprocess.Popen("pwds", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print("错误的返回结果是: %s" % r.stderr.read().decode("utf-8")) # 错误的返回结果是: /bin/sh: pwds: command not found

12.struct模块======>将一个不定长的int数据打包成一个固定4位的bytes类型方法

import struct

b_len = struct.pack("i", 30)
print(b_len) # b'\x1e\x00\x00\x00'
b_len1 = struct.pack("i", 3000)
print(b_len1) # b'\xb8\x0b\x00\x00' num1 = struct.unpack("i", b_len)
print(num1) # (30,)
num2 = struct.unpack("i", b_len1)
print(num2) # (3000,)

13.hmac模块========>md5加密算法方法

import hmac

md5_obj = hmac.new("盐".encode("utf-8"), "用户密码".encode("utf-8"))
r = md5_obj.digest()
print(r) # b'\xa5\xc4#\x19\xa4\xae\x83\x887m\xa5\xaeC\x0c\xc6j'

18_Python常用的模块中的某个方法的更多相关文章

  1. itertools模块中的product方法

    itertools模块中的product方法 itertools.product(*iterables[, repeat]) 笛卡尔积 创建一个迭代器,生成表示item1,item2等中的项目的笛卡尔 ...

  2. Python OS模块中的fork方法实现多进程

    import os '''使用OS模块中的fork方式实现多进程''' '''fork方法返回两次,分别在父进程和子进程中返回,子进程中永远返回0,父进程返回的是子进程的is''' if __name ...

  3. 引入math模块中的log()方法,导致"TypeError: return arrays must be of ArrayType",什么原因?

    from math import log from numpy import *import operator ............ re = log(pro,2) ............ Ty ...

  4. python 查看模块中的方法

    way 1.help() way 2.dir() # dir() 函数不带参数时,返回当前范围内的变量.方法和定义的类型列表: way 3. 使用inspect模块, inspect.getmembe ...

  5. os、os.path模块(文件/目录方法)

    1.模块的概念:模块是一个包含所有定义的变量.函数的文件,模块可以被其余模块调用. 2.利用OS模块实现对系统文件的. os模块中常见的方法: gercwd()     返回当前工作目录 chdir( ...

  6. 测试脚本中的等待方法 alter对话框处理

    测试脚本中的等待方法 等待是为了使脚本执行更加稳定 1. 常用的休眠方式:time模块的sleep方法 2. selenium模块中的等待方法 等待查找5s 查找不到就报错 对登录测试py进行修改 a ...

  7. python模块中的__all__属性

    转自:http://blog.csdn.net/sxingming/article/details/52903377 python模块中的__all__属性,可用于模块导入时限制,如:from mod ...

  8. python基础:os模块中关于文件/目录常用的函数使用方法

    Python是跨平台的语言,也即是说同样的源代码在不同的操作系统不需要修改就可以同样实现 因此Python的作者就倒腾了OS模块这么一个玩意儿出来,有了OS模块,我们不需要关心什么操作系统下使用什么模 ...

  9. os模块中关于文件/目录常用的函数使用方法

    os模块中关于文件/目录常用的函数使用方法 函数名 使用方法 getcwd() 返回当前工作目录 chdir(path) 改变工作目录 listdir(path='.') 列举指定目录中的文件名('. ...

随机推荐

  1. 22、Command 命令模式

    1.command 命令模式 命令模式(Command Pattern):在软件设计中,我们经常需要向某些对象发送请求,但是并不知道请求的接收者是谁,也不知道被请求的操作是哪个,我们只需在程序运行时指 ...

  2. 网络协议: TCP/IP 和UDP/IP

    网络协议: TCP/IP 和UDP/IP TCP/IP TCP/IP(Transmission Control Protocol/Internet Protocol)是一种可靠的网络数据传输控制协议. ...

  3. Web For Pentester 学习笔记 - XSS篇

    XSS学习还是比较抽象,主要最近授权测的某基金里OA的XSS真的实在是太多了,感觉都可以做一个大合集了,加上最近看到大佬的博客,所以这里我也写一个简单的小靶场手册,顺带着也帮助自己把所有XSS的方式给 ...

  4. C#LeetCode刷题之#485-最大连续1的个数(Max Consecutive Ones)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3714 访问. 给定一个二进制数组, 计算其中最大连续1的个数. ...

  5. C#算法设计查找篇之02-二分查找

    二分查找(Binary Search) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/699 访问. 二分查找也称折半查 ...

  6. 性能分析(6)- 如何迅速分析出系统 CPU 的瓶颈在哪里

    性能分析小案例系列,可以通过下面链接查看哦 https://www.cnblogs.com/poloyy/category/1814570.html 前言 在做性能测试时,我们会需要对 Linux 系 ...

  7. 二、JAVA 的了解与安装

    1.java了解 1.1.java三大版本 javaSE:标准版(桌面程序,控制台开发...) javaME:嵌入式开发(手机.小家电...)[可以忽略] javaEE:企业版开发(web端,服务器开 ...

  8. MySQL查看数据存放位置

    show global variables like "%datadir%";

  9. 浏览器自动化的一些体会9 webBrowser控件之零碎问题3

    WebBrowser控件最大的优点是可以轻松嵌入win form程序中,但是微软好像对这个控件没什么兴趣,这么多年了还没有改进,结果造成一堆问题. 1. 不支持https 2. 缺省模拟ie 7,如果 ...

  10. Windows 右键 照片查看器 不见了--解决办法

    桌面新建 一个文本文档,将下边复制进去,另存为命名例如为:1.reg 双击运行1.reg,点‘是’,点确认即可. Windows Registry Editor Version 5.00 ; Chan ...