1、os
all functions from posix, nt or ce, e.g. unlink, stat, etc.
os.name is either 'posix', 'nt' or 'ce'.
os.curdir is a string representing the current directory ('.' or ':')
os.pardir is a string representing the parent directory ('..' or '::')
os.sep is the (or a most common) pathname separator ('/' or ':' or '\\'
os.extsep is the extension separator (always '.')
os.altsep is the alternate pathname separator (None or '/')
os.pathsep is the component separator used in $PATH etc
os.linesep is the line separator in text files ('\r' or '\n' or '\r\n')
os.defpath is the default search path for executables
os.devnull is the file path of the null device ('/dev/null', etc.)
os.environ: 获取操作系统的环境变量值
FUNCTIONS
execl(file, *args)
execle(file, *args)
execlp(file, *args)
execlpe(file, *args)
execvp(file, args)
execvpe(file, args, env)
fdopen(fd, *args, **kwargs)
fsdecode(filename)
fsencode(filename)
popen(cmd, mode='r', buffering=-1) # Supply os.popen()
spawnl(mode, file, *args)
spawnle(mode, file, *args)
spawnlp(mode, file, *args)
spawnlpe(mode, file, *args)
spawnv(mode, file, args)
spawnve(mode, file, args, env)
spawnvp(mode, file, args)
spawnvpe(mode, file, args, env)
chdir(path):改变目录
os.getlogin():取得当前登录用户
os.getpid():取得当前的进程ID; os.getppid():取得当前的进程的父进程ID
os.system("vi dmi.txt") :执行os命令
get_exec_path(env=None)
取环境变量里的PATH值:可以带一个环境变量的值(是一个字典)作为参数;不带的话,取系统的环境变量。
getenv(key, default=None)
取操作系统的某个环境变量值,必须指明一个环境变量名称。注意:linux下大小写敏感。
makedirs(name, mode=511, exist_ok=False)
makedirs(name [, mode=0o777][, exist_ok=False])
递归建立目录:makedirs("./a/b/c/d")。mkdir只能在当前目录下建立单级的目录,makedirs可以一次建立许多级目录
mkdir(path, mode=0o777, *, dir_fd=None)
当前目录下建立单级的目录。
putenv(key, value)
改变或追加环境变量。经过测试,没有写入操作系统中!意外的是,直接修改 os.environ["key"]="value",或将其赋值后修改:s = os.environ; s["key"]="value" 反而可以在 python 里 以 os.getenv("key") 取得刚才设置的值!
removedirs(name)
“递归”删除目录:若有目录 /a/b/c/d/e,发出 removedirs("/a/b/c/d/e"):若e为空,则删除e;此时若d为空,则删除d。依次类推,直至/ (符合条件,直至删除 /a 目录)
renames(old, new)
文件或目录改名
unsetenv(key)
删除环境变量:在windows下失败(win7+python3.4);在linux下执行成功但仍然可以用os.getenv("key")取得前面unsetenv("key")的哪个环境变量
walk(top, topdown=True, οnerrοr=None, followlinks=False)
Directory tree generator.
生成一个目录树。没一级目录返回一个3元的元组:路径,[目录s],[文件s]
当前路径只有一个;但是,可能有多个子目录或没有子目录(为空);可能有多个文件或没有文件(为空)。故,目录s 与 文件s 均为列表。
当前目录的子目录,则在下一个元组里分别列出。
In [101]: fs = os.walk(".\\py")
In [102]: for f in fs:
...: print(f)
...:
('.\\py', ['gtk实例', 'text'], ['fibonacci(生成器).py', 'fibo_dg.py', 'fileop.py', 'gbk.bat', 'getdmi.py', 'getdmiinfo.py', 'getwmiinfo.py','jjb.py'])
('.\\py\\gtk实例', [], ['redemo.py', 'sortvisu.py', 'ss1.py'])
('.\\py\\text', [], ['佳人+no.txt', '佳人.txt', '佳人ansi.txt', '佳人utf8.txt'])
In [103]:
2、sys
import os后,直接以 os.sys.加下面的内容,即可调用sys的相关变量和函数。
静态变量
copyright -- copyright notice pertaining to this interpreter
exec_prefix -- prefix used to find the machine-specific Python library
executable -- absolute path of the executable binary of the Python interpreter
float_info -- a struct sequence with information about the float implementation.
float_repr_style -- string indicating the style of repr() output for floats
hash_info -- a struct sequence with information about the hash algorithm.
hexversion -- version information encoded as a single integer
implementation -- Python implementation information.
int_info -- a struct sequence with information about the int implementation.
maxsize -- the largest supported length of containers.
maxunicode -- the value of the largest Unicode code point
platform -- platform identifier
prefix -- prefix used to find the Python library
thread_info -- a struct sequence with information about the thread implementation.
version -- the version of this interpreter as a string
version_info -- version information as a named tuple
Functions:
displayhook() -- print an object to the screen, and save it in builtins._
excepthook() -- print an exception and its traceback to sys.stderr
exc_info() -- return thread-safe information about the current exception
exit() -- exit the interpreter by raising SystemExit
getallocatedblocks(): 返回当前已分配的内存块数
getdefaultencoding():返回当前的默认字符串编码
getfilesystemencoding():返回操作系统的字符编码:windows==>mbcs, linux==>utf-8
getrecursionlimit(): 返回 递归的最大层数
getswitchinterval()/setswitchinterval(n): 获取/设置python的线程切换时间。系统默认为0.005s。
setprofile(function):返回 function的执行效率分析。
有 部分使用C语言实现的cProfile。下面是cProfile的使用方法:
from cProfile import Profile
import math
......定义测试函数比如test
prof = Profile()
prof.runcall(test)
prof.print_stats()