Python之os模块

在自动化测试中,经常需要查找操作文件,比如查找配置文件(从而读取配置文件的信息),查找测试报告等等,经常会对大量文件和路径进行操作,这就依赖os模块

拓展

绝对路径:在windows中以盘符开头的或者是\开头的我们统称为绝对路径,在linux或者mac中以"/"开头的称之为绝对路径

相对路径:以".“或者”. .“或者直接以文件开头,我们称之为相对路径(”.“当前目录,”. ."代表上一级目录)

常用函数

想要使用os模块,这时就需要先导入os模块,如下:

import os
1. os.getcwd()

功能:获取当前文件所在目录绝对路径

import os

print(os.getcwd())		#C:\Users\HGF\Desktop\test
2. os.listdir(path)

功能:获取指定目录下所有文件的文件名,若不指定path则获取当前目录下的,返回的是列表类型

import os

path1 = r"C:\Users\HGF\Desktop\test"
path2 = "C:\\Users\HGF\Desktop\\test"
print(os.listdir())         #['1.txt', '2.txt', 'b', 'c', 'test.py']
print(os.listdir(path1))    #['1.txt', '2.txt', 'b', 'c', 'test.py']
print(os.listdir(path1))    #['1.txt', '2.txt', 'b', 'c', 'test.py']

注意:path1与path2的写法是等同的

3. os.path.abspath(path)

功能:获取path的绝对路径

import os

print(os.getcwd())
print(os.listdir())
print(os.path.abspath('2.txt'))
print(os.path.abspath('3.txt'))

运行结果如下:

C:\Users\HGF\Desktop\test
['1.txt', '2.txt', 'b', 'c', 'test.py']
C:\Users\HGF\Desktop\test\2.txt
C:\Users\HGF\Desktop\test\3.txt

注意:仅仅是将path与当前文件所在目录的路径进行拼接处理

4. os.path.split(path)

功能:将路径分解为(目录,文件名),返回的是元组类型

import os

print(os.getcwd())
print(os.listdir())
print(os.path.split("."))
print(os.path.split(r"C:\Users\HGF\Desktop\test"))
print(os.path.split(r"C:\Users\HGF\Desktop\test\1.txt"))
print(os.path.split("C:\\Users\\HGF\\Desktop\\test\\"))

运行结果如下:

C:\Users\HGF\Desktop\test
['1.txt', '2.txt', 'b', 'c', 'test.py']
('', '.')
('C:\\Users\\HGF\\Desktop', 'test')
('C:\\Users\\HGF\\Desktop\\test', '1.txt')
('C:\\Users\\HGF\\Desktop\\test', '')

注意:若路径字符串最后一个字符是\,则只有文件夹部分有值,若路径字符串中均无\,则只有文件名部分有值,若路径字符串有\且不在最后,则文件夹和文件名都有值,且返回的结果不包括\

实际上使用""对路径进行切分,从右往左切,切一次,左边获取到的就是目录部分,右边就是文件部分

5. os.path.join(path1,path2,…)

将path进行路径拼接,若其中有绝对路径,则前面的路径不保留,返回的后面的绝对路径

import os

print(os.getcwd())
print(os.listdir())
print(os.path.join(r"C:\Users\HGF\Desktop\test","3.txt"))
print(os.path.join(r"C:\Users\HGF\Desktop\test",r"C:\Users\HGF\Desktop\3.txt"))

运行结果如下:

C:\Users\HGF\Desktop\test
['1.txt', '2.txt', 'b', 'c', 'test.py']
C:\Users\HGF\Desktop\test\3.txt
C:\Users\HGF\Desktop\3.txt
6. os.path.dirname(path)

返回path中文件夹部分,不包括"\"

import os

print(os.getcwd())
print(os.listdir())
print(os.path.dirname(r"C:\Users\HGF\Desktop\test"))
print(os.path.dirname('.'))
print(os.path.dirname(r"C:\Users\HGF\Desktop\test\1.txt"))
print(os.path.dirname(r"C:\Users\HGF\Desktop\test\3.txt"))

运行结果如下:

C:\Users\HGF\Desktop\test
['1.txt', '2.txt', 'b', 'c', 'test.py']
C:\Users\HGF\Desktop

C:\Users\HGF\Desktop\test
C:\Users\HGF\Desktop\test
7. os.path.basename(path)

功能:返回path中文件的部分

import os

print(os.getcwd())
print(os.listdir())
print(os.path.basename(r"C:\Users\HGF\Desktop\test"))
print(os.path.basename('.'))
print(os.path.basename(r"C:\Users\HGF\Desktop\test\1.txt"))
print(os.path.basename(r"C:\Users\HGF\Desktop\test\3.txt"))

运行结果如下:

C:\Users\HGF\Desktop\test
['1.txt', '2.txt', 'b', 'c', 'test.py']
test
.
1.txt
3.txt
8. os.path.getsize(path)

功能: 获取指定路径文件的大小,目录的大小获取不到

import os

print(os.getcwd())
print(os.listdir())
print(os.path.getsize(r"C:\Users\HGF\Desktop\test\test.py"))
print(os.path.getsize(r"C:\Users\HGF\Desktop\test"))
print(os.path.getsize(r"C:\Users\HGF\Desktop\test\b"))

运行结果如下:

C:\Users\HGF\Desktop\test
['1.txt', '2.txt', 'b', 'c', 'test.py']
839
4096
0

注意:获取目录时,有看到返回数值,但目录大小实际不符

9. os.path.exists(path)

功能:判断指定的路径是否存在,若存在则返回True,否则返回False

import os

print(os.listdir(os.getcwd()))
print(os.path.exists("1.txt"))
print(os.path.exists("3.txt"))

运行结果如下:

['1.txt', '2.txt', 'b', 'c', 'test.py']
True
False
10. os.path.isdir(path)

功能:判断指定的路径是否为目录,若是则返回True,否则返回False

import os

print(os.getcwd())
print(os.listdir())
print(os.path.isdir(r"C:\Users\HGF\Desktop\test\test.py"))
print(os.path.isdir(r"C:\Users\HGF\Desktop\test"))

运行结果如下:

C:\Users\HGF\Desktop\test
['1.txt', '2.txt', 'b', 'c', 'test.py']
False
True
11. os.path.isfile(path)

功能:判断指定的路径是否为文件,若是则返回True,否则返回False

import os

print(os.getcwd())
print(os.listdir())
print(os.path.isfile(r"C:\Users\HGF\Desktop\test\test.py"))
print(os.path.isfile(r"C:\Users\HGF\Desktop\test"))

运行结果如下:

C:\Users\HGF\Desktop\test
['1.txt', '2.txt', 'b', 'c', 'test.py']
True
False
12. os.remove(path)

功能:删除指定的文件

import os

print(os.getcwd())
print(os.listdir())
print(os.remove('1.txt'))
print(os.listdir())

运行结果如下:

C:\Users\HGF\Desktop\test
['1.txt', '2.txt', 'b', 'c', 'test.py']
None
['2.txt', 'b', 'c', 'test.py']
13. os.mkdir(path)

功能:创建指定的目录

import os

print(os.getcwd())
print(os.listdir())
print(os.mkdir("d"))
print(os.listdir())

运行结果如下:

C:\Users\HGF\Desktop\test
['2.txt', 'b', 'c', 'test.py']
None
['2.txt', 'b', 'c', 'd', 'test.py']
14. os.rmdir(path)

功能:删除指定的空目录【只能删除一层】

import os

print(os.getcwd())
print(os.listdir())
print(os.rmdir("d"))
print(os.listdir())

运行结果如下:

C:\Users\HGF\Desktop\test
['2.txt', 'b', 'c', 'd', 'test.py']
None
['2.txt', 'b', 'c', 'test.py']
15. os.makedirs(path)

功能:递归创建多层空目录

import os

print(os.getcwd())
print(os.listdir())
os.makedirs("d\e")
print(os.listdir())
print(os.listdir("d"))

运行结果如下:

C:\Users\HGF\Desktop\test
['2.txt', 'b', 'c', 'test.py']
['2.txt', 'b', 'c', 'd', 'test.py']
['e']

16. os.removedirs(path)

功能:递归删除多层空目录【不存在文件】

import os

print(os.getcwd())
print(os.listdir())
print(os.listdir('d'))
os.removedirs("d\e")
print(os.listdir())

运行结果如下:

C:\Users\HGF\Desktop\test
['2.txt', 'b', 'c', 'd', 'test.py']
['e']
['2.txt', 'b', 'c', 'test.py']
17. os.chdir(path)

功能:切换到指定的目录

import os

print(os.getcwd())
print(os.listdir())
os.chdir('b')
print(os.listdir())

运行结果如下:

C:\Users\HGF\Desktop\test
['2.txt', 'b', 'c', 'test.py']
['3.txt', 'd']

需求:递归遍历当前项目下的所有的文件以及目录

import os

def func(path):

    filelist = os.listdir(path)
    if len(filelist) == 0:
        print(path)
    else:
        for file in filelist:
            abspath = os.path.join(path,file)
            if os.path.isfile(abspath):
                print(abspath)
            else:
                func(abspath)

if __name__ == '__main__':

    path = r"C:\Users\HGF\Desktop\test"
    func(path)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值