慢慢积累吧!
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)
用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')
if __name__ == "__main__":
Python 产生子进程的方法有:
import sys
# 获取参数
print sys.argv
# 获取 path
print sys.path
# 获取已载入模块
print sys.modules
# 执行信息,发生异常时可以获取到
print sys.exec_info()
提供文件方法的对象都可以作为标准流:
带 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
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
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有