温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python的os模块怎么使用

发布时间:2022-01-24 16:27:57 来源:亿速云 阅读:139 作者:zzz 栏目:开发技术

本篇内容介绍了“python的os模块怎么使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

os模块提供了多数操作系统的功能接口函数。当os模块被导入后,它会自适应于不同的操作系统平台,根据不同的平台进行相应的操作,在python编程时,经常和文件、目录打交道,这时就离不了os模块。

python的os模块怎么使用

1.当前路径及路径下的文件

os.getcwd():查看当前所在路径。

os.listdir(path):列举目录下的所有文件。返回的是列表类型。

>>> import os
>>> os.getcwd()'D:\\pythontest\\ostest'>>> os.listdir(os.getcwd())
['hello.py', 'test.txt']

2.绝对路径

os.path.abspath(path):返回path的绝对路径。

>>> os.path.abspath('.')'D:\\pythontest\\ostest'>>> os.path.abspath('..')'D:\\pythontest'

3.查看路径的文件夹部分和文件名部分

os.path.split(path):将路径分解为(文件夹,文件名),返回的是元组类型。可以看出,若路径字符串最后一个字符是,则只有文件夹部分有值;若路径字符串中均无,则只有文件名部分有值。若路径字符串有\,且不在最后,则文件夹和文件名均有值。且返回的文件夹的结果不包含.

os.path.join(path2,path3,…):将path进行组合,若其中有绝对路径,则之前的path将被删除。

>>> os.path.split('D:\\pythontest\\ostest\\Hello.py')
('D:\\pythontest\\ostest', 'Hello.py')
>>> os.path.split('.')
('', '.')
>>> os.path.split('D:\\pythontest\\ostest\\')
('D:\\pythontest\\ostest', '')
>>> os.path.split('D:\\pythontest\\ostest')
('D:\\pythontest', 'ostest')
>>> os.path.join('D:\\pythontest', 'ostest')'D:\\pythontest\\ostest'>>> os.path.join('D:\\pythontest\\ostest', 'hello.py')'D:\\pythontest\\ostest\\hello.py'>>> os.path.join('D:\\pythontest\\b', 'D:\\pythontest\\a')'D:\\pythontest\\a'

os.path.dirname(path):返回path中的文件夹部分,结果不包含”

>>> os.path.dirname('D:\\pythontest\\ostest\\hello.py')'D:\\pythontest\\ostest'>>> os.path.dirname('.')''>>> os.path.dirname('D:\\pythontest\\ostest\\')'D:\\pythontest\\ostest'>>> os.path.dirname('D:\\pythontest\\ostest')'D:\\pythontest'

os.path.basename(path):返回path中的文件名。

>>> os.path.basename('D:\\pythontest\\ostest\\hello.py')'hello.py'>>> os.path.basename('.')'.'>>> os.path.basename('D:\\pythontest\\ostest\\')''>>> os.path.basename('D:\\pythontest\\ostest')'ostest'

4.查看文件时间

os.path.getmtime(path):文件或文件夹的最后修改时间,从新纪元到访问时的秒数。

os.path.getatime(path):文件或文件夹的最后访问时间,从新纪元到访问时的秒数。

os.path.getctime(path):文件或文件夹的创建时间,从新纪元到访问时的秒数。

>>> os.path.getmtime('D:\\pythontest\\ostest\\hello.py')
1481695651.857048
>>> os.path.getatime('D:\\pythontest\\ostest\\hello.py')
1481687717.8506615
>>> os.path.getctime('D:\\pythontest\\ostest\\hello.py')
1481687717.8506615

5.查看文件大小

os.path.getsize(path):文件或文件夹的大小,若是文件夹返回0。

>>> os.path.getsize('D:\\pythontest\\ostest\\hello.py')
58L
>>> os.path.getsize('D:\\pythontest\\ostest')
0L

6.查看文件是否存在

os.path.exists(path):文件或文件夹是否存在,返回True 或 False。

>>> os.listdir(os.getcwd())
['hello.py', 'test.txt']
>>> os.path.exists('D:\\pythontest\\ostest\\hello.py')
True
>>> os.path.exists('D:\\pythontest\\ostest\\Hello.py')
True
>>> os.path.exists('D:\\pythontest\\ostest\\Hello1.py')
False

7.一些表现形式参数

os中定义了一组文件、路径在不同操作系统中的表现形式参数,如:

>>> os.sep'\\'>>> os.extsep'.'>>> os.pathsep';'>>> os.linesep'\r\n'

8.实例说明

在自动化测试过程中,常常需要发送邮件,将最新的测试报告文档发送给相关人员查看,这是就需要查找最新文件的功能。

举例:查找文件夹下最新的文件。

python的os模块怎么使用

代码如下:

import os
def new_file(test_dir):
   #列举test_dir目录下的所有文件(名),结果以列表形式返回。   lists=os.listdir(test_dir)
   #sort按key的关键字进行升序排序,lambda的入参fn为lists列表的元素,获取文件的最后修改时间,所以最终以文件时间从小到大排序   #最后对lists元素,按文件修改时间大小从小到大排序。   lists.sort(key=lambda fn:os.path.getmtime(test_dir+'\\'+fn))
   #获取最新文件的绝对路径,列表中最后一个值,文件夹+文件名   file_path=os.path.join(test_dir,lists[-1])
   return file_path#返回D:\pythontest\ostest下面最新的文件print new_file('D:\\system files\\workspace\\selenium\\email126pro\\email126\\report')

运行结果:

python的os模块怎么使用

最后再啰嗦一句,关于lambda的用法(python中单行的最小函数):

key=lambda fn:os.path.getmtime(test_dir+'\\'+fn)#相当于def key(fn):
   return os.path.getmtime(test_dir+'\\'+fn)

“python的os模块怎么使用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI