-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilemanagement.py
More file actions
79 lines (57 loc) · 2.1 KB
/
filemanagement.py
File metadata and controls
79 lines (57 loc) · 2.1 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import os
class ClassFile(object):
def __init__(self):
pass
def delete_file(self,path):
if os.path.exists(path):
try:
os.remove(path)
response = ("True", "File deleted")
except WindowsError, e:
response = "False", e
else:
response = ("False", "File does not exist")
return response
def delete_file_from_folder(self, filename, folder):
return self.delete_file(
os.path.join(os.getcwd(), folder, filename))
def search_file(self, filename):
try:
if os.path.exists(filename):
response = (True, "File Exists")
else:
response = (False, "File does not Exist")
except:
response = (False, "File searching error")
return response
def search_all_files_by_extension(self, folder, ext):
try:
filelist = [x for x in os.listdir(
folder) if x.endswith(ext)]
except WindowsError:
return False, "Folder cannot be found",[]
if len(filelist) > 0:
msg = "Found %d files" % len(filelist)
response = True, msg, filelist
else:
response = False, "No file found",[]
return response
def delete_all_files_by_extension(self, folder, ext):
files=self.search_all_files_by_extension(folder, ext)
try:
original_files = len(files[2])
except IndexError:
return False,"No files found"
final_files = 0
for i in files[2]:
if self.delete_file_from_folder(i, folder)[0]:
final_files += 1
if final_files == 1:
msg = "Deleted 1 file of %d %s file from %s folder" % (original_files, ext, folder)
response = (True, msg)
elif final_files > 1:
msg = "Deleted %d files of %d %s files from %s folder" % (final_files, original_files, ext, folder)
response = True, msg
else:
return False, "No files found"
return response