
python进行系统管理的模块。系统管理模块的介绍和使用。
系统管理的四个模块:
- os
- os.path
- glob
- fnmatch
1.OS
OS模块中包含了普遍的操作系统功能,如果希望程序与平台没有强关联的情况下这个模块很重要,这个模块可以在不改动程序的基础上,使程序运行在Linux和Windows之间。
可移植性说明
1 2 3 4 5
| os.name os.getcwd() os.listdir() os.remove() os.linesep
|
OS模块操作文件
1 2 3 4 5
| os.unlink os.remove os.rmdir os.mkdir os.rename
|
OS模块操作权限
1
| os.access('/etc/fstab', os.R_OK)
|
os.path模块
os.path模块主要是用来拆分路径,构建新的路径,获取文件属性和判断文件的类型使用。
拆分路径
1 2 3 4
| os.path.split() os.path.dirname() os.path.basename() os.path.splitext()
|
构建路径
1 2 3
| os.path.expanduser('~/Liarlee') os.path.abspath('Liarlee.txt') os.path.join(os.path.expanduser('~mysql', 't', 'Liarlee.py'))
|
获取文件属性
1 2 3 4
| os.path.getatime() os.path.getmtime() os.path.getctime() os.path.getsize()
|
判断文件类型
1 2 3 4
| os.path.exists os.path.isfile os.path.isdir os.path.islink
|
fnmatch模块
glob模块