Created
June 28, 2012 23:25
-
-
Save lardissone/3014694 to your computer and use it in GitHub Desktop.
Move or Delete files in specific folders
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from datetime import datetime, timedelta | |
root_destino = 'C:\\backups\\' # windows | |
root_destino = '/Users/leandroardissone/Downloads/test-backup' # unix | |
root = 'D:\\aaaa\\Downloads\\' # windows | |
root = '/Users/leandroardissone/Downloads/' # unix | |
carpetas = [ # relativas, dentro de root | |
'carpeta1', | |
'carpeta2', | |
'carpeta3' | |
] | |
meses = 4 | |
ahora = datetime.now() - timedelta(meses * 365 / 12) | |
def procesar_archivos(carpeta, borrar=False): | |
dirList = os.listdir(carpeta) | |
for fname in dirList: | |
origen = '%s%s%s' % (carpeta, os.sep, fname) | |
if datetime.fromtimestamp(os.path.getmtime(origen)) < ahora: | |
if borrar: | |
print 'BORRADO: %s' % fname | |
os.remove(origen) | |
else: | |
destino = '%s%s%s' % (root_destino, os.sep, fname) | |
print 'MOVIDO: %s' % fname | |
os.rename(origen, destino) | |
else: | |
print 'IGNORADO: %s' % fname | |
# action! | |
for carpeta in carpetas: | |
print '-' * 80 | |
print 'Moviendo carpeta: %s\n' % (root + carpeta) | |
archivo = root + carpeta | |
procesar_archivos(archivo, borrar=False) # si le pones True, borra :) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment