-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexact_duplicate_delete.py
More file actions
executable file
·86 lines (65 loc) · 2.67 KB
/
Copy pathexact_duplicate_delete.py
File metadata and controls
executable file
·86 lines (65 loc) · 2.67 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
#!/usr/bin/env python
"""
File name: exact_duplicate_delete.py
Author: Jim Craveiro <jim.craveiro@gmail.com>
Date: 2/26/2017
Deletes duplicate files found in specified dir and sub-dirs.
"""
import hashlib
import os
import sys
def sha_256_sum(file_path):
"""Reads in a file and returns the sha256 hash of its contents.
Args:
file_path (str): the absolute or relative path to the file being hashed
Returns:
str: sha256 hash value of file contents
"""
hash_buffer = hashlib.sha256()
# reads in the specified file and adds the contents to the data to be hashed
with open(file_path, "rb") as infile:
hash_buffer.update(infile.read())
# hashes data in buffer and returns it
return hash_buffer.hexdigest()
def recursive_parse_dir(target_path, hashes=[]):
"""Deletes duplicate files in specified dir and sub-dirs.
Goes through every item in a specified directory and if it is a file
it hashes its contents and then checks if the hash exists in a list.
If it does not, it adds the hash to the list, otherwise it deletes the
file because it is an exact duplicate. If it is a directory it recursively
calls itself with the new target dir, and the current list of hashes,
returning the new hash list when it is done with a sub-dir.
Args:
target_path (str): the absolute or relative path to be traversed
hashes (list[str]): current list of known files, defaults to an empty list
Returns:
hashes (list[str]): see above
"""
if target_path[-1] != "/": target_path += "/"
dir_contents = os.listdir(target_path)
for item in dir_contents:
full_path = target_path + item
if os.path.isfile(full_path):
file_hash = sha_256_sum(full_path)
# check if the file is a duplicate
if file_hash in hashes:
os.remove(full_path)
else:
hashes.append(file_hash)
elif os.path.isdir(full_path):
hashes = recursive_parse_dir(full_path, hashes)
return hashes
def main():
"""Main function gets command line input and starts the parsing."""
help_text = "Please pass in the target path as an argument."
# don't allow the program to run without a path passed from the command line
if len(sys.argv) <= 1:
print help_text
else:
# allows for help to trigger informative text to be printed to the command line
if sys.argv[1].lower() == "help":
print help_text
else:
recursive_parse_dir(sys.argv[1])
if __name__ == "__main__":
main()