os处理文件

 1 #!/usr/local/bin/python3
 2 # -*- coding:utf-8 -*-
 3 import sys, os
 4 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 5 sys.path.append(BASE_DIR)
 6 
 7 from tools.project_path import *
 8 
 9 import platform
10 class RemoveTagFile(object):
11     path=None
12     def removeFile(self,path,remove_list,retain_list): #path后面要跟/
13         self.path=path
14         system_test=platform.system()
15         if(system_test=='Windows'):
16             path_last=self.path[-1]
17             if(path_last!='\' ):
18                 self.path=self.path+'\'
19         elif(system_test=='Linux'):
20             path_last = self.path[-1]
21             if (path_last != '/'):
22                 self.path = self.path + '/'
23         if(len(remove_list)==0 and len(retain_list)==0):  #如果remove_list,retain_list都是空则删除path目录下所有文件及文件夹
24             self.remove_file(self.eachFile(self.path))
25         elif(len(remove_list)>0 and len(retain_list)==0):
26             self.remove_file(remove_list)
27         elif(len(remove_list)==0 and len(retain_list)>0):
28             list=self.eachFile(self.path)
29             for f in retain_list:
30                 if(f in list):
31                     list.remove(f)
32                 else:
33                     print('There is no file in the directory!')
34             self.remove_file(list)
35         elif (len(remove_list) > 0 and len(retain_list) > 0):
36             for f in retain_list:
37                 if(f in remove_list):
38                     remove_list.remove(f)
39             self.remove_file(remove_list)
40 
41     def remove_file(self,file_list):
42         for filename in file_list:
43             if(os.path.exists(self.path+filename)):   #判断文件是否存在
44                 if(os.path.isdir(self.path+filename)):
45                     self.del_file(self.path+filename)
46                 else:
47                     if(os.path.exists(self.path+filename)):
48                         os.remove(self.path+filename)
49             else:
50                 print(self.path+filename+' is not exist!')
51         for filename in file_list:
52             if(os.path.exists(self.path+filename)):
53                 self.del_dir(self.path+filename)
54     def del_file(self,path):     #递归删除目录及其子目录下的文件
55         for i in os.listdir(path):
56             path_file = os.path.join(path, i) #取文件绝对路径
57             if os.path.isfile(path_file):     #判断是否是文件
58                 os.remove(path_file)
59             else:
60                 self.del_file(path_file)
61     def del_dir(self,path):  #删除文件夹
62         for j in os.listdir(path):
63             path_file = os.path.join(path, j)  # 取文件绝对路径
64             if not os.listdir(path_file):    #判断文件如果为空
65                 os.removedirs(path_file)   #则删除该空文件夹,如果不为空删除会报异常
66             else:
67                 self.del_dir(path_file)
68     def eachFile(self,filepath):   #获取目录下所有文件的名称
69         pathDir = os.listdir(filepath)
70         list=[]
71         for allDir in pathDir:
72             child = os.path.join('%s%s' % (filepath, allDir))
73             fileName=child.replace(filepath,'')
74             list.append(fileName)
75             return list
76 if __name__ == '__main__':
77     rtf=RemoveTagFile()
78     #以下表示只删除D:Test目录下的a文件夹、a.txt文件、b.txt文件
79     """
80     规则:
81     1、如果remove_list、retain_list都为空则删除path目录下所有文件及文件夹
82     2、如果remove_list为空、retain_list不为空,则删除不在retain_list中的所有文件及文件夹
83     3、如果remove_list不为空、retain_list为空,则删除在remove_list中的所有文件及文件夹
84     4、如果remove_list、retain_list都不为空,则删除不在retain_list中且在remove_list中的所有文件及文件夹
85     """
86     # path = 'D:Test'
87     # remove_list = ['a', 'a.txt', 'b.txt']  # 要删除的文件名称
88     # retain_list = ['c.txt']  # 要保留的文件名称
89     # rtf.removeFile(path,remove_list,retain_list)
90     rtf.del_file(report_path)
原文地址:https://www.cnblogs.com/guoyuanping/p/12107827.html