python学习4 常用内置模块
2016-03-15 21:25
746 查看
logging
os
路径处理// 获取当前路径 os.path.abspath(__file__) //获取当前文件夹路径 os.path.dirname(os.path.abspath(__file__)) os.path.abspath('.') //路径拼接处理 os.path.join(path1, path2)
创建链接
//创建硬/文件链接 os.link('oops.txt', 'yikes.txt') //创建符号链接 os.symlink('oops.txt', 'jeepers.txt') //检查文件还是符号链接 os.path.islink('jeepers.txt') //False //获取符号链接路径 os.path.realpath('jeepers.txt')
获取进程信息
import os os.getegid() os.getcwd()
shutil
复制内容import shutil shutil.copy('oops.txt', 'ohno.txt')
glob
列出匹配文件import glob print(glob.glob('*'))
sys
获取执行参数sys.argv
subprocess
执行命令subprocess.Popen(command)
time
使用sleep
try: while True: print('start') time.sleep(2) print('end') except KeyboardInterrupt: print('stop')
事件处理
//获取当前秒 time.time()
datetime
通过时间戳获取日期from datetime import datetime dt = datetime.fromtimestamp(t) dt.year ;dt.month ;dt.day
inspect 检查运行模块的一些基本信息
判断generator函数from inspect import isgeneratorfunction isgeneratorfunction(fab)
获取参数
inspect.signature(fn)
types
判断generator函数和generator实例import types isinstance(fab, types.GeneratorType) isinstance(fab(5), types.GeneratorType)
pickle
把对象序列化成bytes//把对象序列化成bytes byte_data = pickle.dumps({"name": "jinks"}) //反操作 pick.loads(byte_data)
json
例子Python对象和JSON的转化
json_str = json.dumps(data) data = json.loads(json_str)
functools 高阶函数相关的模块
消除装饰器带来__name__改变的副作用def decorator(func): @wraps(func) def wrapper(*args, **kwargs): return func(*args, **kwargs) return wrapper @decorator def add(x, y): return x + y
urllib, urllib2 处理url相关操作的库
分析http查询字符串urllib.parse.parse_qs 返回字典 urllib.parse.parse_qsl 返回列表
collections 内建的集合模块
deque实现插入删除操作的双向列表from collections import deque q = deque(['a', 'b', 'c']) q.append('x') q.appendleft('y')
heapq 实现堆排序
查找最值import heapq nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2] print(heapq.nlargest(3, nums)) # Prints [42, 37, 23] print(heapq.nsmallest(3, nums)) # Prints [-4, 1, 2] // portfolio = [ {'name': 'IBM', 'shares': 100, 'price': 91.1}, {'name': 'AAPL', 'shares': 50, 'price': 543.22}, {'name': 'FB', 'shares': 200, 'price': 21.09}, {'name': 'HPQ', 'shares': 35, 'price': 31.75}, {'name': 'YHOO', 'shares': 45, 'price': 16.35}, {'name': 'ACME', 'shares': 75, 'price': 115.65} ] cheap = heapq.nsmallest(3, portfolio, key=lambda s: s['price']) expensive = heapq.nlargest(3, portfolio, key=lambda s: s['price'])
re
split 字符串分割from re import split line = 'asdf fjdk; afed, fjek,asdf, foo' rs = split(r'[;,\s]\s*', line)
相关文章推荐
- PythonChallenge Mission 11
- Python ImportError: No module named XXX
- python中import失败解决的简单办法
- Python错误处理
- 一行 Python 实现并行化 -- 日常多线程操作的新思路 - 左手键盘,右手书 - SegmentFault
- python from multiprocessing import Pool,Process,Value,Array,Manager
- EigenFace的使用 python
- python 之socket语法及相关
- Type 3字体的解决办法
- Python琐碎知识总结
- 中文维基百科上的word2vec实验,python及java版本
- Python3缩进对逻辑的影响
- Python3缩进对逻辑的影响
- Python正则表达式,re模块
- OpenGL with PyOpenGL Python and PyGame p.4-Moving the player automatically towards the cube
- Python——编写函数装饰器
- python execfile()和locals()函数
- Python变量和对象类型速记手册
- The python debugger调试(PDB)的简介
- python 文件和路径操作函数小结