码迷,mamicode.com
首页 > 其他好文 > 详细

singlefile.py

时间:2017-05-16 14:49:24      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:ase   split   move   min   from   folder   file   proc   code   

#! 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

标签:ase   split   move   min   from   folder   file   proc   code   

原文地址:http://www.cnblogs.com/jzssuanfa/p/6860767.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!