singlefile.py

来源:互联网 发布:godaddy新加坡注册域名 编辑:程序博客网 时间:2024/06/05 07:15
#! encoding=utf-8import osimport os.pathAllFiles = {}MinSize = 8100def OneDir( DirName ):    if DirName[len(DirName)-1] <> "\\":        DirName = DirName + "\\"    tmpFiles = os.listdir( DirName )    for OneFile in tmpFiles:        OneFilePath = DirName + OneFile        if os.path.isdir( DirName + OneFile ):            OneDir( DirName + OneFile + "\\")        else:            statinfo = os.stat( OneFilePath )            if AllFiles.has_key( statinfo.st_size ):                OneKey = AllFiles[ statinfo.st_size ]                OneKey.append( OneFilePath )                AllFiles[ statinfo.st_size ] = OneKey            else:                if statinfo.st_size  > MinSize:#                    print statinfo.st_size                    FileSize = statinfo.st_size                    AllFiles[ FileSize ] = [ OneFilePath ]#                    print AllFiles#                    print AllFiles.keys()import logging   # 创建一个loggerlogger = logging.getLogger('mylogger')logger.setLevel(logging.DEBUG)   # 创建一个handler,用于写入日志文件fh = logging.FileHandler('test.log')fh.setLevel(logging.DEBUG)   # 再创建一个handler,用于输出到控制台ch = logging.StreamHandler()ch.setLevel(logging.DEBUG)   # 定义handler的输出格式formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')fh.setFormatter(formatter)ch.setFormatter(formatter)   # 给logger添加handlerlogger.addHandler(fh)logger.addHandler(ch)   # 记录一条日志#logger.info('foorbar')#该代码片段来自于: http://www.sharejs.com/codes/python/6248import pythoncomfrom win32com.shell import shellfrom win32com.shell import shellcondef MakeShortcut( lnkname, filename ):    logger.info(lnkname +' is linked to '+filename)    os.remove( lnkname )    shortcut = pythoncom.CoCreateInstance(    shell.CLSID_ShellLink, None,    pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink)    shortcut.SetPath(filename)#    shortcut.SetIconLocation(iconname,0)#可有可无,没有就默认使用文件本身的图标#    if os.path.splitext(lnkname)[-1] != '.lnk':    lnkname += ".lnk"    shortcut.QueryInterface(pythoncom.IID_IPersistFile).Save(lnkname,0)    import filecmpdef FileCompare( SameSizeFiles ):    CompareResult = [[]]*len(SameSizeFiles)    for i in range( 1, len(SameSizeFiles) ):        for j in range( i ):           if filecmp.cmp(SameSizeFiles[i],SameSizeFiles[j]):               if len( CompareResult[ j ] ) > 1:                   CompareResult[ j ].append( i )               else:                   CompareResult[ j ] = [ i, j ]               break    for i in range( len(SameSizeFiles)-1 ):        if len( CompareResult[ i ] ) > 1:            for j in range( len( CompareResult[ i ] )-1 ):                MakeShortcut( SameSizeFiles[ CompareResult[ i ][j]],                    SameSizeFiles[ CompareResult[i][ len( CompareResult[ i ] )-1 ] ] )                                     ##############  start #############     DirList = []while cmp(input,"#")<>0:    input = raw_input("please input folder names, \"#\" to finish input:")    DirList.append(input)#    print DirListDirList.pop()for Dir in DirList:    OneDir( Dir )print AllFiles.keys()for EachSize in AllFiles.keys():    if (len( AllFiles[EachSize] ) > 1) & ( EachSize > MinSize ):        FileCompare( AllFiles[EachSize] )


0 0