Python 代码片段总结
慢慢积累吧!
升级PIP
python -m pip install --upgrade pip
代理
easy_install -i https://pypi.douban.com/simple/ requests
pip install -i https://pypi.douban.com/simple/ requests
文件头
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# or
#! /usr/bin/env python
# coding=utf-8
遍历文件夹
import os
for root, dirs, files in os.walk(top, topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
运行时相关
# 当前文件夹
os.getcwd()
# 当前脚本路径
file_path = os.path.realpath(__file__)
# 当前脚本文件夹
os.path.dirname(file_path)
生成pyc文件
用py_compile模块
import py_compile
py_compile.compile(file_path)
也可以直接执行命令转换当前所有py文件,使用到compileall模块
python -m compileall xxx.py
python -m compileall .
生成的pyc可以跨平台使用,但是只能这样用python xxx.pyc
,而不能使用./xxx.pyc
执行,因为缺少了shebang的支持,不过貌似Binfmt_misc可以解决这个问题,ubuntu下apt-get install binfmt-support
可以直接安装。
文件操作高级封装
10.10. shutil — High-level file operations
环境变量
import os
for k, v in os.environ.items():
print "%s => %s" % (k, v)
# Note Calling putenv() directly does not change os.environ, so it’s better to modify os.environ.
os.getenv('PATH')
os.putenv('GO15VENDOREXPERIMENT', '0')
main的写法
if __name__ == "__main__":
子进程
Python 产生子进程的方法有:
- os.popen
- subprocess.popen
- os.system
- os.fork
- os.spawnv
- os.fork
sys
import sys
# 获取参数
print sys.argv
# 获取 path
print sys.path
# 获取已载入模块
print sys.modules
# 执行信息,发生异常时可以获取到
print sys.exec_info()
标准流
- sys.stdin
- sys.stdout
- sys.stderr
提供文件方法的对象都可以作为标准流:
带 read() 方法的对象可以赋值给 sys.stdin; read() 为输入流提供数据 带 write() 方法的对象可以赋值给 sys.stdout; 所有标准输出都发给 write() 方法。
class Input(object):
def read(self):
return 'hello, world\n'
class Output(object):
def write(self, s):
f = open('1.txt', 'w')
f.write(s)
f.close()
sys.stdin = Input()
sys.stdin.read() # 'hello, world'
sys.stderr = Input()
sys.stderr.read() # 'hello, world'
sys.stdout = Output()
sys.stdout.write('hello, world\n') #生成文件 1.txt,输入文本 'hello, world\n'
版本比较
Python的版本比较有很多种方法,StrictVersion、LooseVersion还有NormalizedVersion。不过在 PEP 386已经不推荐使用StrictVersion和LooseVersion了。
Note that LooseVersion and StrictVersion have been deprecated under PEP 386 and will at some point be replaced by NormalizedVersion.
安装verlib
pip install -i https://pypi.douban.com/simple/ verlib
使用版本比较的功能。
from verlib import NormalizedVersion
NormalizedVersion("1.2b1") < NormalizedVersion("1.2") #True
检查pid是否在运行
How to check if there exists a process with a given pid?
import errno
import os
import sys
def pid_exists(pid):
"""Check whether pid exists in the current process table.
UNIX only.
"""
if pid < 0:
return False
if pid == 0:
# According to "man 2 kill" PID 0 refers to every process
# in the process group of the calling process.
# On certain systems 0 is a valid PID but we have no way
# to know that in a portable fashion.
raise ValueError('invalid PID 0')
try:
os.kill(pid, 0)
except OSError as err:
if err.errno == errno.ESRCH:
# ESRCH == No such process
return False
elif err.errno == errno.EPERM:
# EPERM clearly means there's a process to deny access to
return True
else:
# According to "man 2 kill" possible error values are
# (EINVAL, EPERM, ESRCH)
raise
else:
return True
Renference:
学习Python Compare version strings Working with versions PEP 386 -- Changing the version comparison module in Distutils