企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] <br> ### os模块介绍 os模块主要用于程序与操作系统之间的交互。 在os.py源码中,有一段说明很重要,如下: ```python r"""OS routines for NT or Posix depending on what system we're on. This exports: - all functions from posix, nt or ce, e.g. unlink, stat, etc. - os.path is either posixpath or ntpath - 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.) Programs that import and use 'os' stand a better chance of being portable between different platforms. Of course, they must then only use functions that are defined by all platforms (e.g., unlink and opendir), and leave all pathname manipulation to os.path (e.g., split and join). """ ``` 从这段描述可知,在python我们使用相同的os模块与不同的平台,如windows或linux交互。 os模块中有非常多的变量和方法,这里就不一一介绍,还是那句话,`源码才是最好的学习资料`。 ### os 模块常用变量 ```python >>> os.name # 显示当前系统名称,win->'nt'; Linux->'posix' 'nt' >>> os.sep # 显示当前系统中,路径分割符 '\\' >>> os.linesep # 显示当前系统的文本换行符 '\r\n' >>> os.environ # 显示系统环境变量 ``` ### os 模块常用方法 ```python >>> os.getcwd() # 注意:是当前运行脚本的路径,不是python脚本文件的路径 'D:\\os_exec' # 与目录相关的方法 >>> os.mkdir('tmp') # 创建文件夹tmp,如果tmp已存在,则会报错哦 >>> os.rmdir('tmp') # 删除空文件夹,如果文件夹不为空或不存在,都会报错哦 >>> os.makedirs('tmp4/tmp44') # 创建多级文件夹tmp4/tmp44 >>> os.removedirs('tmp4/tmp44') # 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推 >>> os.rename('src','dst')# 修改文件(夹)名称,从src修改为dst >>> os.listdir() # 返回目录下的所有文件(夹)列表 ['dst', 'tmp1', 'tmp2', 'tmp5', '新建文本文档.txt'] # 与调用系统命令有关 >>> os.system('dir') # 在子shell中运行shell命令,直接显示,返回值0表示成功,1表示失败 # 与文件相关的方法 >>> os.remove('tmp.txt') # 删除文件 ``` **删除非空文件夹:** 有心的朋友或许已经发现,在删除文件夹时,如果文件夹非空,则用os是无法删除的。此时可以使用`shutil`库,该库为python内置库,是一个对文件及文件夹高级操作的库,可以与os库互补完成一些操作,如文件夹的整体复制,移动文件夹,对文件重命名等。 ```python shutil.rmtree(path) #递归删除文件夹 ``` ### os.path 模块常用方法 ```python >>> os.path.abspath("tmp1") # 返回path规范化的绝对路径 'D:\\os_exec\\tmp1' >>> os.path.split("D://os_exec/test.txt") # 将path分割成目录和文件名二元组返回 ('D://os_exec', 'test.txt') >>> os.path.dirname("D://os_exec/test.txt") # 返回path的目录,同os.path.split(path)的第一个元素 'D://os_exec' >>> os.path.basename("D://os_exec/test.txt") # 返回path最后的文件名(如果path以/或\结尾则返回空值)同os.path.split(path)的第二个元素 'test.txt' >>> os.path.exists("D://os_exec/test.txt") # 判断path是否存在 True >>> os.path.isfile("D://os_exec/test.txt") # 判断path是否文件 True >>> os.path.isdir("D://os_exec/test.txt") # 判断path是否文件夹 False >>> os.path.join("D:\\os_exec","tmp","test.txt") # 将多个路径组合后返回 'D:\\os_exec\\tmp\\test.txt' >>> os.path.getatime('D:\\os_exec\\test.txt') # 获取path新增时间 1530950642.685528 ``` <hr style="margin-top:100px"> :-: ![](https://box.kancloud.cn/2ff0bc02ec938fef8b6dd7b7f16ee11d_258x258.jpg) ***微信扫一扫,关注“python测试开发圈”,了解更多测试教程!***