Created
February 13, 2014 23:59
-
-
Save Lbatson/8986563 to your computer and use it in GitHub Desktop.
file and directory utilities
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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