Python3内置模块

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, onerror=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]:

 

猜你喜欢

转载自www.cnblogs.com/Afisher/p/9391989.html