Created
September 19, 2020 11:43
-
-
Save angristan/c210ea47926f307e479ccffbb6ecd84f to your computer and use it in GitHub Desktop.
Fix PNG file date from file name
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
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