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 osos.getegid()os.getcwd()
shutil
import shutilshutil.copy('oops.txt', 'ohno.txt')
glob
import globprint(glob.glob('*'))
sys
sys.argv
subprocess.Popen(command)
try: while True: print('start') time.sleep(2) print('end')except KeyboardInterrupt: print('stop')
//获取当前秒time.time()
datetime
from datetime import datetimedt = datetime.fromtimestamp(t)dt.year ;dt.month ;dt.day
inspect 检查运行模块的一些基本信息
from inspect import isgeneratorfunction isgeneratorfunction(fab)
inspect.signature(fn)
types
- 判断generator函数和generator实例
import types isinstance(fab, types.GeneratorType) isinstance(fab(5), types.GeneratorType)
pickle
//把对象序列化成bytesbyte_data = pickle.dumps({"name": "jinks"})//反操作pick.loads(byte_data)
json_str = json.dumps(data)data = json.loads(json_str)
def decorator(func): @wraps(func) def wrapper(*args, **kwargs): return func(*args, **kwargs) return wrapper@decoratordef add(x, y): return x + y
处理url相关操作的库
urllib.parse.parse_qs 返回字典urllib.parse.parse_qsl 返回列表
内建的集合模块
from collections import dequeq = deque(['a', 'b', 'c'])q.append('x')q.appendleft('y')
实现堆排序
import heapqnums = [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
from re import splitline = 'asdf fjdk; afed, fjek,asdf, foo'rs = split(r'[;,\s]\s*', line)