Skip to content

Instantly share code, notes, and snippets.

@enghqii
Last active July 7, 2016 09:30
Show Gist options
  • Save enghqii/006713d108ebae5af319 to your computer and use it in GitHub Desktop.
Save enghqii/006713d108ebae5af319 to your computer and use it in GitHub Desktop.
import os
import time
import datetime
currentPath = os.path.dirname(os.path.abspath(__file__))
filenames = os.listdir(currentPath)
for filename in filenames:
# full file path of files in current directory
fullFilePath = os.path.join(currentPath, filename)
# for files
if os.path.isfile(fullFilePath):
# not for me
if fullFilePath == os.path.abspath(__file__):
continue
try:
fileCTime = os.path.getctime(fullFilePath)
cdatetime = datetime.datetime.fromtimestamp(fileCTime)
#print("src : " + fullFilePath)
destPath = os.path.join(
currentPath,
str(cdatetime.year),
str(cdatetime.month)
)
#print("dest : %s" % (destPath))
if not os.path.exists(destPath):
os.makedirs(destPath)
os.rename(fullFilePath, os.path.join(destPath, filename))
except FileExistsError as exception:
print("FileExistsError occured : " + filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment