查找目录中同名的文件或者文件夹

import os
import os.path
rootdir="/Volumes/extend/test" #要查找的目录

result = []
def findSame(parent,filenames,category):
    for filename in filenames:
        print filename
        for filename2 in filenames:
            if cmp(filename,filename2) != 0:
                if cmp(filename.lower(), filename2.lower()) == 0:
                    message = category + parent + " / " + filename + "==" + filename2
                    if not message in result:
                        result.append(message)

print "start find..."
for parent,dirnames,filenames in os.walk(rootdir):
    for dirname in dirnames:
        print parent
        findSame(parent,dirnames,"find folder ")
        #print  "dirname is:  " + dirname

    #for filename in filenames:
        #print "parent is: " + parent
        #print "filename is: " + filename
    findSame(parent,filenames,"find file ")

print "done.\n"

if len(result) > 0:
    print "result is:"
    for message in result:
        print message
else:
    print "no same file or folder found."

查找Linux、MAC下同名文件和文件夹。比如 Folder1和folder1 、filename1和Filename1。

posted @ 2016-01-23 23:39  Liren  阅读(2622)  评论(0编辑  收藏  举报