Skip to content

Commit da19c8e

Browse files
authored
Merge pull request #1 from NYPD/v0.0.1
V0.0.1
2 parents 05c6cad + 6d94c71 commit da19c8e

File tree

6 files changed

+146
-1
lines changed

6 files changed

+146
-1
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,10 @@ venv.bak/
102102

103103
# mypy
104104
.mypy_cache/
105+
106+
### VisualStudioCode ###
107+
.vscode
108+
.history
109+
110+
# Pycharm
111+
.idea

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,20 @@
1-
# Phodupe
1+
# phodupe
2+
3+
## What is it
4+
5+
A simple little python script that finds duplicate file names (without the extension) across two directories and gives the user the option to delete them.
6+
7+
## Why
8+
9+
I used this as an opportunity to begin learning python. It was created for a very very specific scenario and might not be too useful to anyone else.
10+
11+
## How do to install / run
12+
13+
(There are probobly better ways to run this, but i'm a python noob)
14+
15+
- Navigate to the folder you downloaded the git project to
16+
- cd \development\git\Phodupe\
17+
- Install the python script
18+
- python .\setup.py install
19+
- Run the script by passing two directories
20+
- python phodupe "C:\Users\NYPD\Desktop\OG Phone Walls" "C:\Users\NYPD\Desktop\2Delete"

phodupe/__init__.py

Whitespace-only changes.

phodupe/__main__.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import sys
2+
from phodupe.dupe_finder import DupeFinder
3+
4+
5+
def main():
6+
7+
destination1 = sys.argv[1]
8+
destination2 = sys.argv[2]
9+
10+
dupe_files = DupeFinder.getDuplicateFileNames(destination1, destination2)
11+
12+
if len(dupe_files) is 0:
13+
print('No dupe files found!')
14+
exit()
15+
16+
user_input = input("{} duplicate file names found. Enter 'y' to delete or 'n' to abort:\n".format(len(dupe_files)))
17+
18+
if user_input == 'y':
19+
DupeFinder.deleteFiles(dupe_files, destination1, destination2)
20+
print('Files deleted, exiting...')
21+
else:
22+
print('No files deleted, aborting...')
23+
exit()
24+
25+
26+
if __name__ == "__main__":
27+
main()

phodupe/dupe_finder.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import os
2+
import pathlib
3+
import glob
4+
import itertools
5+
6+
class DupeFinder:
7+
"""
8+
Simple utility class for finding and manipulating duplicate files
9+
"""
10+
11+
@staticmethod
12+
def getDuplicateFileNames(directory1, directory2):
13+
"""
14+
Return a list of duplicate file name "stems" in both directories.
15+
16+
e.g. :
17+
18+
directory1 : [a.png, b.png]
19+
20+
directory2 : [a.png, b.png]
21+
22+
returns: [a, b]
23+
24+
Parameters
25+
----------
26+
directory1 : str
27+
Path of the first directory
28+
directory2 : str
29+
Path of the second directory
30+
31+
Returns
32+
-------
33+
list
34+
A list of the duplicate file names between both directories. If no
35+
duplicate files are found, an empty list is returned
36+
"""
37+
directory1Path = pathlib.Path(directory1)
38+
directory2Path = pathlib.Path(directory2)
39+
40+
directory1FilesNoExt = []
41+
directory2FilesNoExt = []
42+
43+
for file in os.listdir(directory1Path):
44+
purePath = pathlib.Path(os.path.join(directory1, file))
45+
directory1FilesNoExt.append(purePath.stem)
46+
for file in os.listdir(directory2Path):
47+
purePath = pathlib.Path(os.path.join(directory2, file))
48+
directory2FilesNoExt.append(purePath.stem)
49+
50+
dupeFiles = []
51+
52+
for fileName in directory1FilesNoExt:
53+
if fileName in directory2FilesNoExt:
54+
dupeFiles.append(fileName)
55+
56+
return dupeFiles
57+
58+
@staticmethod
59+
def deleteFiles(fileNames, directory1, directory2):
60+
"""
61+
Deletes the files provided in both directories
62+
63+
Parameters
64+
----------
65+
fileNames : list
66+
List of string files names to delete
67+
directory1 : str
68+
Path of the first directory
69+
directory2 : str
70+
Path of the second directory
71+
"""
72+
for file in fileNames:
73+
74+
directory1Path = os.path.join(directory1, file)
75+
directory2Path = os.path.join(directory2, file)
76+
77+
directory1Glob = glob.glob('{}.*'.format(directory1Path))
78+
directory2Glob = glob.glob('{}.*'.format(directory2Path))
79+
80+
for filePath in itertools.chain(directory1Glob, directory2Glob):
81+
os.remove(filePath)

setup.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from setuptools import setup
2+
3+
setup(
4+
name='phodupe',
5+
version='v0.0.1',
6+
packages=['phodupe'],
7+
url='https://github.com/NYPD/phodupe',
8+
license='MIT',
9+
author='NYPD',
10+
description='Duplicate file finder'
11+
)

0 commit comments

Comments
 (0)