Skip to content

Commit

Permalink
Merge pull request #1 from NYPD/v0.0.1
Browse files Browse the repository at this point in the history
V0.0.1
  • Loading branch information
NYPD authored Jun 9, 2019
2 parents 05c6cad + 6d94c71 commit da19c8e
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,10 @@ venv.bak/

# mypy
.mypy_cache/

### VisualStudioCode ###
.vscode
.history

# Pycharm
.idea
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
# Phodupe
# phodupe

## What is it

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.

## Why

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.

## How do to install / run

(There are probobly better ways to run this, but i'm a python noob)

- Navigate to the folder you downloaded the git project to
- cd \development\git\Phodupe\
- Install the python script
- python .\setup.py install
- Run the script by passing two directories
- python phodupe "C:\Users\NYPD\Desktop\OG Phone Walls" "C:\Users\NYPD\Desktop\2Delete"
Empty file added phodupe/__init__.py
Empty file.
27 changes: 27 additions & 0 deletions phodupe/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import sys
from phodupe.dupe_finder import DupeFinder


def main():

destination1 = sys.argv[1]
destination2 = sys.argv[2]

dupe_files = DupeFinder.getDuplicateFileNames(destination1, destination2)

if len(dupe_files) is 0:
print('No dupe files found!')
exit()

user_input = input("{} duplicate file names found. Enter 'y' to delete or 'n' to abort:\n".format(len(dupe_files)))

if user_input == 'y':
DupeFinder.deleteFiles(dupe_files, destination1, destination2)
print('Files deleted, exiting...')
else:
print('No files deleted, aborting...')
exit()


if __name__ == "__main__":
main()
81 changes: 81 additions & 0 deletions phodupe/dupe_finder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import os
import pathlib
import glob
import itertools

class DupeFinder:
"""
Simple utility class for finding and manipulating duplicate files
"""

@staticmethod
def getDuplicateFileNames(directory1, directory2):
"""
Return a list of duplicate file name "stems" in both directories.
e.g. :
directory1 : [a.png, b.png]
directory2 : [a.png, b.png]
returns: [a, b]
Parameters
----------
directory1 : str
Path of the first directory
directory2 : str
Path of the second directory
Returns
-------
list
A list of the duplicate file names between both directories. If no
duplicate files are found, an empty list is returned
"""
directory1Path = pathlib.Path(directory1)
directory2Path = pathlib.Path(directory2)

directory1FilesNoExt = []
directory2FilesNoExt = []

for file in os.listdir(directory1Path):
purePath = pathlib.Path(os.path.join(directory1, file))
directory1FilesNoExt.append(purePath.stem)
for file in os.listdir(directory2Path):
purePath = pathlib.Path(os.path.join(directory2, file))
directory2FilesNoExt.append(purePath.stem)

dupeFiles = []

for fileName in directory1FilesNoExt:
if fileName in directory2FilesNoExt:
dupeFiles.append(fileName)

return dupeFiles

@staticmethod
def deleteFiles(fileNames, directory1, directory2):
"""
Deletes the files provided in both directories
Parameters
----------
fileNames : list
List of string files names to delete
directory1 : str
Path of the first directory
directory2 : str
Path of the second directory
"""
for file in fileNames:

directory1Path = os.path.join(directory1, file)
directory2Path = os.path.join(directory2, file)

directory1Glob = glob.glob('{}.*'.format(directory1Path))
directory2Glob = glob.glob('{}.*'.format(directory2Path))

for filePath in itertools.chain(directory1Glob, directory2Glob):
os.remove(filePath)
11 changes: 11 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from setuptools import setup

setup(
name='phodupe',
version='v0.0.1',
packages=['phodupe'],
url='https://github.com/NYPD/phodupe',
license='MIT',
author='NYPD',
description='Duplicate file finder'
)

0 comments on commit da19c8e

Please sign in to comment.