-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFH.py
More file actions
176 lines (131 loc) · 4.9 KB
/
Copy pathDFH.py
File metadata and controls
176 lines (131 loc) · 4.9 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import sys
import os
import collections
import re
from hashlib import md5
args = sys.argv
if len(args) != 2:
print("Directory is not specified")
else:
counter = 0
total_size = 0
size_after_file_del = 0
size = {}
d = collections.defaultdict(dict)
file_number = {}
# foldername = args[1]
# path = r"C:\Users\rahul\PycharmProjects\DuplicateFileHandlersProject1of1\module\root_folder"
path = args[1]
#r"C:\Users\rahul\PycharmProjects\DuplicateFileHandlersProject1of1\module\root_folder"
print('Enter file format:')
file_format = input()
print()
print('Size sorting options:\n1. Descending\n2. Ascending\n')
while True:
print("Enter a sorting option:")
sorting_option = int(input())
print()
if sorting_option != 1 and sorting_option != 2:
print()
print('Wrong option')
continue
else:
break
# os.chdir(foldername)
os.chdir(path)
# for root, dirs, files in os.walk(foldername, topdown=True):
for root, dirs, files in os.walk(path, topdown=True):
for name in files:
if name.endswith(file_format):
size[os.path.join(root, name)] = os.path.getsize(os.path.join(root, name))
d[os.path.getsize(os.path.join(root, name))][os.path.join(root, name)] = md5(
open(os.path.join(root, name), 'rb').read()).hexdigest()
total_size += os.path.getsize(os.path.join(root, name))
if sorting_option == 2:
set_of_values = collections.OrderedDict(sorted(d.items()))
for i in set_of_values:
print(i, 'bytes')
for key, value in size.items():
if value == i:
print(key)
print()
else:
set_of_values = collections.OrderedDict(sorted(d.items(), reverse=True))
for i in set_of_values:
print(i, 'bytes')
for key, value in size.items():
if value == i:
print(key)
print()
def compute_hash(si, i):
global counter
res = collections.Counter(si.values())
temp = []
count = 0
for a, b in res.items():
if b > 1:
if count != 1:
print()
print(i, 'bytes')
count += 1
print("Hash: ", a)
for key, value in si.items():
if a == value:
counter += 1
print(f'{counter}. {key}')
file_number[counter] = key
while True:
print('Check for duplicates?')
check_dup = input()
print()
if check_dup == 'yes':
for i in set_of_values:
compute_hash(set_of_values[i], i)
break
elif check_dup == 'no':
break
else:
print()
print('Wrong option')
continue
def members(dictarg, keyslistarg):
count = 0
for list_item in keyslistarg:
if int(list_item) in dictarg:
count += 1
return len(keyslistarg) == count
while True:
delete_files = input('\nDelete files?\n')
if delete_files == 'yes':
while True:
try:
print('\nEnter file numbers to delete:')
file_sequence = input().split()
for item in file_sequence:
if re.match(r'[\D?\.]+', item):
pass
elif '.' in item:
# print(item)
pass
if not file_sequence:
print('\nWrong format')
continue
elif members(file_number, file_sequence):
for number in file_sequence:
size_after_file_del += os.path.getsize(file_number.get(int(number)))
print("Deleting:", file_number.get(int(number)))
print('\nTotal freed up space:', total_size - size_after_file_del, ' bytes')
sys.exit()
else:
print('\nWrong format')
# print("All files Dictionary", d)
# print("Size Dictionary", size)
# print("File Number Dictionary", file_number)
except ValueError:
print('\nWrong format')
elif delete_files == 'no':
break
else:
print()
print('Wrong option')
continue