Skip to content

Instantly share code, notes, and snippets.

@diegorocha
Created September 20, 2017 18:18
Show Gist options
  • Save diegorocha/a18bed6dbe5333681f617e22a28be606 to your computer and use it in GitHub Desktop.
Save diegorocha/a18bed6dbe5333681f617e22a28be606 to your computer and use it in GitHub Desktop.
#!/bin/env python
# coding: utf-8
from __future__ import print_function
from re import compile
from glob import glob
from os import path, rename
from datetime import datetime
# Parâmetros de execução do script
SOURCE_PATH = '/home/diegorocha/teste'
DESTINATION_PATH = '/home/diegorocha/teste2'
DATE_REGEX = r'(\d{4}-\d{2}-\d{2})' # AAAA-MM-DD
DATE_FORMAT = '%Y-%m-%d' # AAAA-MM-DD
DAYS = 30
# Código do script
pattern = compile(DATE_REGEX)
file_filter = path.join(SOURCE_PATH, '*.log')
today = datetime.today()
for fp in glob(file_filter):
result = pattern.search(fp)
if result:
date_str = result.groups()[0]
file_date = datetime.strptime(date_str, DATE_FORMAT)
file_age = today - file_date
if file_age.days >= DAYS:
# Arquivo será movido
_, file_name = path.split(fp)
new_file_destination = path.join(DESTINATION_PATH, file_name)
rename(fp, new_file_destination)
print('Arquivo', fp, 'movido para', DESTINATION_PATH)
else:
print('Arquivo', fp, 'é recente')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment