Skip to content

Instantly share code, notes, and snippets.

@matolg
Last active December 11, 2016 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matolg/99fba4eaa0e6263dd54df3934ce84096 to your computer and use it in GitHub Desktop.
Save matolg/99fba4eaa0e6263dd54df3934ce84096 to your computer and use it in GitHub Desktop.
Rename all files in directory with modification date
from os import listdir, rename
from os.path import isfile, join, getmtime, splitext
from time import gmtime, strftime
pathToFiles = "E:/test"
nameFormat = "%d%m%Y_%H%M%S"
files = [f for f in listdir(pathToFiles) if isfile(join(pathToFiles, f))]
for f in files:
originFullPath = join(pathToFiles, f)
originFileName, fileExtension = splitext(f)
createdTime = getmtime(originFullPath)
newFileName = strftime(nameFormat, gmtime(createdTime)) + fileExtension
newFullPath = join(pathToFiles, newFileName)
rename(originFullPath, newFullPath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment