操作系统接口
os模块提供了不少与操作系统相关联的函数。
>>> import os >>> os.getcwd() # 返回当前的工作目录 'C:\\Python34' >>> os.chdir('/server/accesslogs') # 修改当前的工作目录 >>> os.system('mkdir today') # 执行系统命令 mkdir 0
建议使用 "import os" 风格而非 "from os import *"。这样能保证随操作系统不同而有所变化的 os.open() 不会覆盖内置函数 open()。
在使用 os 这样的大型模块时内置的 dir() 和 help() 函数非常有用:
>>> import os >>> dir(os) <returns a list of all module functions> >>> help(os) <returns an extensive manual page created from the module's docstrings>
针对日常的文件和目录管理任务,:mod:shutil 模块提供了1个易于使用的高级接口:
>>> import shutil >>> shutil.copyfile('data.db', 'archive.db') >>> shutil.move('/build/executables', 'installdir')
文件通配符
glob模块提供了1个函数用于从目录通配符搜索中生成文件列表:
>>> import glob >>> glob.glo