-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathremover.py
60 lines (52 loc) · 1.45 KB
/
remover.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/5/30 10:23 上午
# @Author : CrissChan
# @Site : https://blog.csdn.net/crisschan
# @File : remove.py
# @Intro : 删除文件或者目录
import os
import shutil
class Remover(object):
@classmethod
def dir(cls,rm_root):
'''
递归删除目录以及目录内的所有内容.
:param root:删除目录
:return:
'''
for root, dirs, files in os.walk(rm_root, topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
os.removedirs(rm_root)
@classmethod
def dir_under(cls,rm_root):
'''
:param rm_root:删除目录下所有的文件,目录不删除存在
:return:
'''
try:
shutil.rmtree(rm_root)
os.makedirs(rm_root)
except:
return
@classmethod
def file(cls,rm_file):
'''
删除文件
:param root:删除文件路径
:return:
'''
if os.path.exists(rm_file):
os.unlink(rm_file)
@classmethod
def dir_empty(cls,rm_root):
'''
递归删除目录,如果有一个目录非空则会抛出异常
:param rm_root: 删除目录
:return:
'''
if os.path.exists(rm_root):
os.removedirs(rm_root)