-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindduplicatefiles.py
More file actions
31 lines (26 loc) · 1.11 KB
/
Copy pathfindduplicatefiles.py
File metadata and controls
31 lines (26 loc) · 1.11 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
import os
import sys
import subprocess
def getSum( targetDir, extension ): # ( Directory to be walked ( e.g. ~/ ) , file extensions ( e.g. ".py" )
# get absolute full path to get 'os.walk' to work
fullDir = os.path.expanduser( targetDir )
if not os.path.isabs( fullDir ):
fullDir = os.path.abspath( fullDir )
fileDict = dict()
# walk path
for path, dirnames, filenames in os.walk( fullDir ) :
for name in filenames :
a_file_path = path + '/' + name # full path for each files walked
if ( a_file_path[ -len( extension ) : ] == extension ):
md5sum = ( subprocess.check_output( [ "md5sum", a_file_path ], universal_newlines=True ) ) [ : 32 ]
list_to_add = fileDict.get( md5sum, [] )
list_to_add.append( a_file_path )
fileDict[ md5sum ] = list_to_add
return fileDict
if __name__ == "__main__":
sums = getSum( "~/Documents", ".py" )
for key in sums:
if len( sums[key] ) > 1 :
print( "duplicate files:" )
for path in sums[key]:
print( path )