# ---------------------------------------------------------------- # cleanupRenderFolders # # Bernard Lebel # Directeur technique, rendering # Action Synthese, Marseille (France) # Aout 2004 # # # Description # This script is meant to remove the content of Backup and system folders # from XSI project folders. After some time these folders take a lot of space, # so you might need to clean them. # # # Installation # Put into C:\PythonXX\Lib # Remove "_public" from the file name. # # # Usage # Launch a Python interpreter # Run: # import cleanupRenderFolders # Answer simulation question # ---------------------------------------------------------------- # Import block import os import shutil as s # ---------------------------------------------------------------- # Main function def cleanup( sRoot, bSim ): # Traverse down the root path for sDirPath, aDirs, aFiles in os.walk( sRoot, True, None ): for sDir in aDirs: # -------------------------- # Backup folders # Check if currently visited folder is named Backup if sDir == 'Backup': # If so, visit it bottom up for sBackDirPath, aBackDirs, aBackFiles in os.walk( os.path.join( sDirPath, sDir ), False ): # Remove files for sBackFile in aBackFiles: if bSim == 'y': print 'File to be removed: ' + os.path.join( sBackDirPath, sBackFile ) else bSim: os.remove( os.path.join( sBackDirPath, sBackFile ) ) # Remove directories for sBackDir in aBackDirs: if bSim == 'y': print 'Folder to be removed: ' + os.path.join( sBackDirPath, sBackDir ) else: os.rmdir( os.path.join( sBackDirPath, sBackDir ) ) # -------------------------- # system folders # Check if currently visited directory is named system if sDir == 'system': for sSysDirPath, aSysDirs, aSysFiles in os.walk( os.path.join( sDirPath, sDir ), False ): for sSysFile in aSysFiles: if not sSysFile == 'dsprojectlist': if bSim == 'y': print 'File to be removed: ' + os.path.join( sSysDirPath, sSysFile ) else: os.remove( os.path.join( sSysDirPath, sSysFile ) ) for sSysDir in aSysDirs: if bSim == 'y': print 'Folder to be removed: ' + os.path.join( sSysDirPath, sSysDir ) else: os.rmdir( os.path.join( sSysDirPath, sSysDir ) ) # Request input from user # In case you have several main directories you want to check, you can include inputs # that will in the end will call for the function to be performed on each inputs. # Below is an example #bMagic = str( raw_input( 'Drive1? [y/n]' ) ) # The simulation thing is there if you don't want the take chances. # You say yes, it will print the to be deleted folders and files # instead of removing them. bSim = str( raw_input( 'Simulation? [y/n]' ) ) # Create empty list to store root paths aRoots = [] # Specify path to cleanup here: sPath = aRoots.append( sPath ) # Check if roots were specified if len( aRoots ) < 1: print 'No root specified, script aborted.' else: # Check if proper values entered for Simulation if bSim != 'y' and bSim != 'n': print 'You specified a wrong parameter for Simulation, script will stop.' else: # Iterate list of roots for sRoot in aRoots: # Call main function cleanup( sRoot, bSim )