Skip to content

Commit c7f0b69

Browse files
authored
Merge pull request #288 from TaniaMalhotra/remove_duplicate_files
Added script to remove duplicate files
2 parents 92f0feb + 5dccc42 commit c7f0b69

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Remove duplicate files from directory
2+
- - - - - - - - - - - - -
3+
## Aim
4+
Aim of the script is to remove duplicate files from a directory.
5+
6+
## To run
7+
- Run ```remove.py```
8+
- Enter the path to the directory or sub-directory in which you want to delete duplicate files</br>
9+
10+
## Example
11+
The current directory contains ```file1.txt``` and ```file2.txt``` that have the same contents. </br>
12+
Running ```remove.py``` will remove ```file2.txt``` as it is a duplicate of file1.txt
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file is for testing
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file is for testing
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# python program to remove duplicate files from directory
2+
import os
3+
import hashlib
4+
import glob
5+
#taking path of directory as user input
6+
PATH_directory = input("Please enter the path of directory in which you wish to remove duplicate files : ")
7+
unique_files = []
8+
for file in os.listdir(PATH_directory):
9+
# checking if path is existing file
10+
if os.path.isfile(file):
11+
hash = hashlib.md5(open(file, 'rb').read()).hexdigest()
12+
# if hash of the file in consideration is unique
13+
if hash not in unique_files:
14+
unique_files.append(hash)
15+
# delete file if it has a matching hash
16+
else:
17+
print("Found a duplicate. Now removing duplicate file")
18+
os.remove(file)

0 commit comments

Comments
 (0)