Created
September 25, 2021 10:01
-
-
Save gengen1988/98486723ba8dc53d931b9c542bde040a to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
import math | |
import os | |
import shutil | |
import time | |
TODAY_DIR = r'F:\fences-portals\today' | |
WEEK_DIR = r'F:\fences-portals\this-week' | |
ARCHIVES_DIR = r'F:\fences-portals\archives' | |
SECONDS_TO_DAYS = 1 / 3600 / 24 | |
def move_old_files(src, dest, target_days): | |
today_time = time.time() | |
for item in os.listdir(src): | |
file_path = os.path.join(src, item) | |
modification_time = os.path.getmtime(file_path) | |
elapsed_time = today_time - modification_time | |
elapsed_days = math.floor(elapsed_time * SECONDS_TO_DAYS) | |
print(f'> "{item}" created {elapsed_days} days') | |
if elapsed_days > target_days: | |
print(f'move "{item}" to "{dest}"') | |
try: | |
shutil.move(file_path, dest) | |
except Exception as e: | |
print(f'could not move {file_path}. {e}') | |
def main(): | |
print('=== moving today files ===') | |
move_old_files(TODAY_DIR, WEEK_DIR, 1) | |
print('') | |
print('=== moving week files ===') | |
move_old_files(WEEK_DIR, ARCHIVES_DIR, 7) | |
print('') | |
print('=== done ===') | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment