Last active
April 11, 2019 00:15
-
-
Save AntoniosHadji/f600119bdaff8273791f1797e0d9ce42 to your computer and use it in GitHub Desktop.
archive log files
This file contains 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
#!/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