python_常用标准库及第三库函数汇总

function

print(func.__code__.co_argcount) #参数个数
print(func.__code__.co_varnames) #局部变量名列表
print(func.__code__.co_consts) #局部声明常量列表
print(func.__code__.co_code) #源码2进制
print(func.__code__.co_name,func.__name__) #函数名
print(func.__code__.co_names) #调用函数列表
sys._getframe().f_code.co_name # 在内部取函数名

ctypes

ctypes.CDLL("./test.so") 返回对象用于调用c

re

re.search(pattern,string,flags).group(1) 返回第一个括号匹配字符串

inspect

inspect.getsource(object):返回对象源码
inspect.getargspec(func):表示函数的参数
inspect.getfullargspec(func):表示函数的参数,针对keyword-only parameters
inspect.getabsfile(object, _filename=None):返回对象文件路径

sys

sys.path:返回列表,包含模块搜索路径
sys.path.append(r'E:\workplace\'):添加搜索路径
sys.argv[0]:返回当前文件abs路径
sys.stdin.encoding:返回系统默认编码
sys._getframe().f_code.co_name:返回当前函数名

os

os.system(cmd):执行调用shell
os.popen(cmd, mode='r', buffering=-1):调用shell,返回结果的文件流
os.mkdir(path):创建目录
os.makedirs(path):mkdir -p
os.path.abspath(__file__):返回当前文件abs路径
os.path.basename(__file__):返回当前文件名
os.path.dirname(__file__):当前文件所在路径
os.path.exists('test.txt'): 判断文件是否存在
os.remove('test.txt'):删除文件
os.removedirs('dirt'):删除目录
os.rename(name,newname):修改文件名称
os.listdir():返回列表,包含当前文件路径的所有文件名称
os.getcwd() 获取工作路径
os.chdir(path) 改变工作路径
os.path.exists(patn) 判断存在性
os.path.isfile(path)是文件
os.path.isdir(path)时目录
os.path.join(path1,path2)链接字符串为路径
os.path.getsize(path) 获取文件大小,可以判断空文件

shutil

`shutil.copy(src,des)`复制文件
`shutil.copytree(src,des)`复制目录
`shutil.move(src,des)`移动文件或目录
`shutil.rmtree('dir')`删除目录

random

random.choice([1,2,3]):随意返回指定列表的数据

pyperclip

pyperclip.copy(str):把字符串复制到剪切板
pyperclip.paste():返回当前剪切板内容

itertools

ct=itertools.count(1):返回从1开始的自增构造器,用next(ct)读取下一变量
itertools.combinations(arr,leng):返回包含列表所有组合可能的迭代去

collections

collections.defaultdict(int):返回值被初始化为int类型的字典,未声明的键均可以读取
collections.Counter([1,2,3,3]).most_common(2):对列表数据进行计量,most_common让Counter对象返回执行项数的列表

faker

faker.Factory.create().user_agent() :返回一个User-Agent

hashlib

hashlib.md5().update(''.encode('utf-8')):生产md5

字符串格式

'{0}-{1}-{2}'.format(0,1,2):格式化字符串
'{a}-{b}-{c}'.format(a=1,b=2,c=3):格式化字符串
'%s-%s-%s'%(1,2,3):格式化字符串

编码

bytes('string','utf-8'):把字符串编码为二进制
'string'.encode('utf-8'):把字符串编码为二进制
bt.decode('utf-8'):把二进制解码为可读写字符串
chardet.detect(bt)['encoding']:获取编码方式

其他

__name__:返回当前模块执行过程中的名称,当前程序运行在该模块中,等于__main__否则模块名
__file__:返回当前文件abs路径
type(value):返回value得类型
with open('test.txt','rb',encoding='utf-8') as file::局部执行文件读写

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值