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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_39532362/article/details/88091511

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

sys

sys.path.append(r'E:\workplace\'):指定路径加入环境变量
sys.argv[0]:返回当前文件abs路径
sys.stdin.encoding:返回系统默认编码

os

os.path.abspath(__file__):返回当前文件abs路径
os.path.basename(__file__):返回当前文件名
os.path.exists('test.txt'): 判断文件是否存在
os.remove('地址.txt'):删除文件
os.rename(name,newname):修改文件名称
os.listdir():返回列表,包含当前文件路径的所有文件名称

random

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

pyperclip

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

itertools

ct=itertools.count(1):返回从1开始的自增构造器,用next(ct)读取下一变量

collections

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

字符串格式

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

编码

bytes('string','utf-8'):把字符串编码为二进制
'string'.encode('utf-8'):把字符串编码为二进制
bt.decode('utf-8'):把二进制解码为可读写字符串

其他

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

猜你喜欢

转载自blog.csdn.net/weixin_39532362/article/details/88091511