风一更--软件开发--python 常用代码

目的:记录平时常用的 python 功能代码:


2022-12-06 

场景: 批量删除指定目录下, 剔除混杂无用的文件.

依赖: os / path / dit;

关联: command DP, 将做什么,与怎么做分离。 不同的层级.

 

 

from p1_dec_06_22_deleteSpecifyFiles.DeleteTargetFilesCmd import DeleteTargetFilesCmd

if __name__ == "__main__":
    targetDirPath = r'C:\Users\GritS\Desktop\study records\spring-batch'
    deleteRelated  = DeleteTargetFilesCmd(targetDirPath)
    deleteRelated.execute()
View Code
import os

class DeleteTargetFilesCmd:
    __targetDirPath = ""

    def __init__(self, targetDirPath):
        self.__targetDirPath = targetDirPath

    def execute(self):
        self.deleteMultiFiles(self.__targetDirPath);

    def deleteMultiFiles(self, targetDir):
        for fileItem in os.listdir(targetDir):
            fileFullPath = os.path.join(self.__targetDirPath, fileItem)
            if self.isMatchFile(fileFullPath):
                os.remove(fileFullPath)

    def isMatchFile(self, fileFullPath):
        isFile = os.path.isfile(fileFullPath)
        isMOVFile = os.path.splitext(fileFullPath)[-1] == ".MOV"
        if isFile and isMOVFile:
            return True
        else:
            return False
View Code

 

posted @ 2022-12-06 09:35  君子之行  阅读(7)  评论(0)    收藏  举报