Skip to content

Instantly share code, notes, and snippets.

@kampfgnu
Last active July 31, 2020 21:21
Show Gist options
  • Save kampfgnu/bd9c137d510085fbe510ab04f1d9e67a to your computer and use it in GitHub Desktop.
Save kampfgnu/bd9c137d510085fbe510ab04f1d9e67a to your computer and use it in GitHub Desktop.
import os, time
from stat import * # ST_SIZE etc
from os import walk
import datetime
extensions = [".jpeg", ".jpg", ".png", ".mov", ".aae", ".mp4"]
files = []
for (dirpath, dirnames, filenames) in walk("./"):
files.extend(filenames)
break
for filename in files:
try:
st = os.stat(filename)
except IOError:
print "failed to get information about", file
else:
# print "file size:", st[ST_SIZE]
# modified = time.asctime(time.localtime(st[ST_MTIME]))
name, extension = os.path.splitext(filename)
extension = extension.lower()
if extension not in extensions:
continue
# print "file: " + name + extension
lastModified = st[ST_MTIME]
date = datetime.datetime.fromtimestamp(lastModified)
# format = '%Y-%m-%d %H.%M.%S.%f'
format = '%Y-%m-%d %H.%M.%S'
dateString = date.strftime(format)#[:-3]
if filename.startswith(dateString):
continue
newName = dateString + extension
counter = 1
while os.path.isfile(newName):
newName = dateString + "_" + str(counter) + extension
counter = counter+1
os.rename(filename, newName)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment