Python自动化测试之日志清理

示例:

import os
class DeleteLogFile:
    def __init__(self,path_1,path_2):
        self.path_1 = path_1
        self.path_2 = path_2
    def delete_file(self):
        for path in [self.path_1,self.path_2]:
            all_log_files = os.listdir(path)
            all_log_files.sort()
            for num in range(len(all_log_files)):
                os.remove(os.path.join(path,all_log_files[num]))

1、首先导入os模块

import os

2、定义类删除日志文件

class DeleteLogFile:

3、定义初始化函数并将其参数实例化

 def __init__(self,path_1,path_2):
        self.path_1 = path_1   # 路径1    系统中需要删除的文件路径有两个
        self.path_2 = path_2   # 路径2    系统中需要删除的文件路径有两个

4、定义删除文件的函数

    def delete_file(self):
        for path in [self.path_1,self.path_2]:
            all_log_files = os.listdir(path)    #获取for循环中路径下的日志文件,且以列表形式打开
            all_log_files.sort()    #排序处理
            for num in range(len(all_log_files)):
                os.remove(os.path.join(path,all_log_files[num]))    #删除日志文件

5、函数调用

test_log= DeleteLogFile(r"{}".format(logs_dir), r"{}".format(screenshot_dir))      #传参两个路径
test_log.delete_file()

版权声明:本文为yutian8233原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。