一.引言

清理无关文件时整理了很多待删除文件,通过 python os 模块进行快速判断与删除。

二.删除

rootDIr 为待删除文件、文件夹的根目录,deleteFile 文件内为要删除的文件、文件夹名称,通过 os.remove 删除文件,os.rmdir 删除文件夹达到快速删除📃的目的。

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os

rootDir = 'root'

with open('deleteFile') as f:
    for line in f.readlines():
        path = rootDir + line.strip()
        if os.path.exists(path) and os.path.isfile(path):
            os.remove(path)
            print("deleteFile:" + path)
        elif os.path.exists(path) and os.path.isdir(path):
            os.rmdir(path)
            print('deleteDir:' + path)
        else:
            print('Path not Exists!')