-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_duplicate_file.py
executable file
·54 lines (48 loc) · 1.46 KB
/
find_duplicate_file.py
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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
find duplicate file
"""
import os
from collections import Counter
import sys
def get_all_files(abspath):
"""
@abspath: get all files from path of 'abspath'
@return: get list of all files
"""
all_files = []
for _, _, files in os.walk(abspath):
for file in files:
all_files.append(file)
return all_files
def find_duplicate_file(abspath):
"""
@path: Direct path
"""
__all_files__ = dict(Counter(get_all_files(abspath)))
for key in __all_files__:
if __all_files__[key] > 1:
file_abspaths = find_file_abspath(abspath, key)
print('Duplicate times:\t'+ str(__all_files__[key]))
print("Duplicate file name:\t" + key)
print("Duplicate file path:")
for file_abspath in file_abspaths:
print(" " + file_abspath)
print('\n')
print("-" * 100)
def find_file_abspath(direct_path, direct_file):
"""find_file_abspath"""
result = []
for dirpath, _, allfiles in os.walk(direct_path):
separator = "" if dirpath[len(dirpath) - 1] == "/" else "/"
for file in allfiles:
if file == direct_file:
result.append(dirpath + separator + file)
return result
# get path from command
__relpath__ = os.getcwd()
if len(sys.argv) > 1:
__relpath__ = sys.argv[1]
__abspath__ = os.path.abspath(__relpath__)
find_duplicate_file(__abspath__)