Skip to content

Instantly share code, notes, and snippets.

@iakat
Created July 26, 2023 15:30
Show Gist options
  • Save iakat/06a0b09de28d936eb1b219a33ac4376d to your computer and use it in GitHub Desktop.
Save iakat/06a0b09de28d936eb1b219a33ac4376d to your computer and use it in GitHub Desktop.
archive.py
# warning! name me archive.py
# Copyright 2023 katia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import time
import os
import shutil
import re
SECONDS_IN_DAY = 24 * 60 * 60
src = '.'
now = time.time()
before = now - SECONDS_IN_DAY
def get_appropriate_folder(filename):
return time.strftime('%Y-%m', time.gmtime(os.path.getmtime(filename)))
for fname in os.listdir(src):
if fname == 'archive.py' or re.match('^[0-9]{4}-[0-9]{2}$', fname):
continue
print('Processing ', fname)
src_fname = os.path.join(src, fname)
folder = get_appropriate_folder(src_fname)
os.makedirs(folder, exist_ok=True)
shutil.move(src_fname, folder)
print('Moved {fname} to {folder}'.format(fname=fname, folder=folder))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment