python2.0で指定したファイルを削除(移動)するプログラム
ブログにプログラムを張ってみるテスト
起動するとバーッと削除(移動)したいファイルがリストアップされます
そのあと「y」押すだけで実行できます
# -*- coding: utf-8 -*- #import module import os import os.path import sys import shutil # define value targetPath = "F:\Library\Music" # path movePath = "F:\_moved" searchWord = " 1." # search word #define function #========================================================== # getDeleteFileList def getFileList( fileDir ) : #print( "getDeleteFileList : " + fileDir ) tmpFileList = os.listdir( fileDir ) #print( "listLength : " + len( tmpFileList ) ) resultFileList = [] for file in tmpFileList : filePath = fileDir + "\\" + file if os.path.isfile( filePath ): #print( filePath ) resultFileList.append( filePath ) elif os.path.isdir( filePath ): resultFileList += getFileList( filePath ) #recursive #else : #print( " invalid file name : " + filePath ) return resultFileList #========================================================== # pickUpToList def pickUpToList( list , word ) : resultFileList = [] #print( "list size " + str( len(list) ) ) #return resultFileList for file in list : if( str( file ).find( word ) != -1 ) : #print( file ) resultFileList.append( file ) return resultFileList #========================================================== # execDelete def execDelete( list ) : if( raw_input( 'excuteDelete ... y > ' ) == 'y' ) : for file in list : os.remove( file ) #========================================================== # execMove def execMove( list ) : if( raw_input( 'excuteMove ... y > ' ) == 'y' ) : for file in list : os.stat( file ) shutil.move( file , movePath ) #========================================================== # program start # is main ? if( not __name__ == "__main__" ) : print("not main.") print("finish program.") sys.exit(0) # targetPath check if( not os.path.isdir( targetPath ) ) : print("targetPath is invalid.") print("finish program.") sys.exit(0) print( "-- start -- " ) fileList = pickUpToList( getFileList( targetPath ) , searchWord ) #print( fileList ) for file in fileList : print( file ) execDelete( fileList ) #execMove( fileList ) print( "-- finish --" )
itunesの曲がいくつかかぶっていたのでで試しに作ってみました