首页 > 代码库 > singlefile.py
singlefile.py
#! encoding=utf-8 import os import os.path AllFiles = {} MinSize = 8100 def 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 # 创建一个logger logger = 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加入handler logger.addHandler(fh) logger.addHandler(ch) # 记录一条日志 #logger.info(‘foorbar‘) #该代码片段来自于: http://www.sharejs.com/codes/python/6248 import pythoncom from win32com.shell import shell from win32com.shell import shellcon def 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 filecmp def 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 DirList DirList.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] )
singlefile.py
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。