Skip to content

Instantly share code, notes, and snippets.

@AntoniosHadji

AntoniosHadji/log_path.py

Last active Apr 11, 2019
Embed
What would you like to do?
archive log files
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
from pathlib import Path
import datetime
import time
print(os.path.dirname(__file__))
print(type(os.path.dirname(__file__)))
print(os.path.dirname(os.path.abspath(__file__)))
print(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
print("-------pathlib-----------")
print(1,Path(__file__))
print(2,Path(__file__).parent)
for p in Path(__file__).resolve().parents:
print(3,p)
print(4,Path('.'))
print(5,Path(__file__).resolve())
print(6,Path(__file__).resolve().parent)
print(7,Path(__file__).resolve().parent.parent)
print(8,type(Path(__file__)))
base = Path(__file__).resolve().parent
Path(base, 'logs/archive').mkdir(parents=True, exist_ok=True)
for p in Path(base, 'logs').rglob('*.*'):
age = int((time.time() - p.stat().st_mtime)/86400)
print('{}:\t {} days old'.format(p, age))
if p.is_file():
if age > 65:
p.unlink()
elif age > 1:
p.rename(base / 'logs/archive' / p.name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment