Skip to content

Instantly share code, notes, and snippets.

@Lbatson
Created February 13, 2014 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lbatson/8986563 to your computer and use it in GitHub Desktop.
Save Lbatson/8986563 to your computer and use it in GitHub Desktop.
file and directory utilities
import sys
import getopt
import os
import string as str
def findDuplicateFilenames(dir):
unique, dupes = [], []
try:
for filename in os.listdir(dir):
name = str.split(filename, '.')[0]
if name not in unique:
unique.append(name)
else:
dupes.append(name)
if len(dupes) > 0:
dupes.sort()
for i in dupes:
print i
except Exception:
print "Path not found. Make sure you type the full path.\n"\
"ex: -f /home/user/dir"
def main(argv=None):
if argv is None:
argv = sys.argv
try:
shortOpts = "f:h"
longOpts = ["find=","help"]
opts, args = getopt.getopt(argv[1:], shortOpts, longOpts)
except getopt.error, msg:
print msg
return 2
for opt, arg in opts:
if opt == "-f" or opt == "--find":
dir = arg
findDuplicateFilenames(dir)
elif opt == "-h" or opt == "--help":
print " -f --find <path>"\
" displays duplicate file names in given path"
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment