Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@angristan
Created September 19, 2020 11:43
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 angristan/c210ea47926f307e479ccffbb6ecd84f to your computer and use it in GitHub Desktop.
Save angristan/c210ea47926f307e479ccffbb6ecd84f to your computer and use it in GitHub Desktop.
Fix PNG file date from file name
import datetime
import os
import re
import sys
import time
import piexif
def fix(directory):
print(directory)
for dirpath, _, filenames in os.walk(directory):
for f in filenames:
fullPath = os.path.abspath(os.path.join(dirpath, f))
# Format: Screenshot_20170204-200801.png
if re.match(r"^Screenshot_\d\d\d\d\d\d\d\d-\d\d\d\d\d\d.*", f):
match = re.search("^Screenshot_(\d\d\d\d)(\d\d)(\d\d)-(\d\d)(\d\d)(\d\d).*", f)
year = int(match.group(1))
month = int(match.group(2))
day = int(match.group(3))
hour = int(match.group(4))
minute = int(match.group(5))
second = int(match.group(6))
date = datetime.datetime(year=year, month=month, day=day, hour=hour, minute=minute, second=second)
modTime = time.mktime(date.timetuple())
print(f, date)
os.utime(fullPath, (modTime, modTime))
if __name__ == "__main__":
fix(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment