python操作系统接口模块:OS
OS模块提供了很多与操作系统进行交互的函数,比如常见的使用函数有获取当前工作目录:os.getcwd();修改当前工作目录:os.chdir(),在系统执行command命令:os.system()。既然是对操作系统的交互,操作系统常见的就有多种,比如unix和windows的。OS模块里有些函数就是只有unix能使用,比如返回父进程的进程id:getppid();有些是unix和windows的都能使用,比如:获取当前进程id:getpid()。这些在官方文档都有说明,如下:
os.getpid()
Returnthecurrentprocessid.
Availability:Unix,Windows.
os.getppid()
Returntheparent’sprocessid.
Availability:Unix.
OS模块确实实用性比较强,在编写自动化测试脚本的时候,有时候也能使用到。就简答说下我使用过的应用场景吧。有时候跑自动化的时候,一些配置文件可能需要重新修改,这时工具远程连接到服务器后,需要让服务器自动替换文件,再重启服务,so~~os模块应用上场了。OS模块主要实现了以下三大功能:进程的管理,操作系统的管理,文件和目录的管理。基于文件的操作,实际python的bulit-in模块也有个open函数,os也有open函数,但是两者之间是有区别的,os的open打开文档后,没有拿到当前文件的对象,返回的是一个整型数值,不便于后面对文档的各种操作。而内置库提供的open函数,返回的是文件的对象。
如果每天要对文件和目录进行管理,推荐使用shutil模块,这个模块提供了更多方便的接口对文件,目录进行管理(复制文件或者目录,删除文件或者目录)。比如如果要删除文件,如果继续使用os,则os.system('rf-rm/build/executables/installdir'),使用shutil模块,就简单了:shutil.move('/build/executables','installdir')。
文件通配符:glob模块
glob模块提供了一个函数用于从目录通配符搜索中生成文件列表。如果,从当前目录中,搜素所有的.PY文件。之前在我不知道glob模块时,我用的是os.path模块实现的。
os.path.splitext(file)[1]=='.py'
现在使用glob模块,可以更灵活的进行处理:
>>>importglob
>>>glob.glob('*.py')
['primes.py','random.py','quote.py']
以上内容为大家介绍了python操作系统接口模块:OS,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
python中ndarray与list转换的方法
python中ndarray与list转换的方法在python数据类型中,list元素可以任意类型组合,而ndarray元素类型必须相同,但是ndarray可以更方便的对多维...详情>>
2023-11-10 08:51:14
python中OrdereDict如何使用?
python中OrdereDict如何使用?我们在对数据进行处理时,如果能够使它们保存顺序的排序,那么用起来会非常的便利。不过字典本身就是没有顺序的,...详情>>
2023-11-10 07:05:00
python中如何使用scipy.fftpack模块计算快速傅里叶变换?
python中如何使用scipy.fftpack模块计算快速傅里叶变换?在编程中,快速傅里叶变换是工程中非常有价值的一类算法,它可以将时域和频域的信号相互...详情>>
2023-11-10 05:50:23
zip()函数如何在python中遍历多个列表?
zip()函数如何在python中遍历多个列表?下面我们就python中zip的说明、语法、使用注意点进行讲解,然后带来遍历多个列表的实例。1、说明zip()函...详情>>
2023-11-10 05:38:33热门推荐
python concat函数有何用法?
沸python里函数装饰器怎么使用?
热如何实现python中的continue语句?
热python中的冒泡排序是什么?
新python中str函数是什么
如何使用python实现项目进度显示?
python中ndarray与list转换的方法
python复制文件的方法整理
Python中numpy数组如何添加元素
python有几种可视化图形库?
python中xlwings是什么?
python上下文管理器的基本介绍
Python自动化测试基础必备知识点一
python中OrdereDict如何使用?
技术干货






