Last active
April 7, 2024 14:38
-
-
Save sirianni/0b4bdac87e62bf136bfa71828827ca43 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
# https://gist.github.com/sirianni/0b4bdac87e62bf136bfa71828827ca43 | |
import os | |
import shutil | |
import time | |
for filename in os.listdir(): | |
if os.path.isfile(filename): | |
# Use mtime as it's more reliable | |
# creation_timestamp = os.stat(filename).st_ctime | |
creation_timestamp = os.stat(filename).st_mtime | |
creation_time = time.localtime(creation_timestamp) | |
year = str(creation_time.tm_year) | |
year_directory = os.path.join('../', year) | |
if not os.path.exists(year_directory): | |
os.makedirs(year_directory) | |
shutil.move(filename, os.path.join(year_directory, filename)) | |
print(f"Moved '{filename}' to {year_directory}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment