Created
November 21, 2016 22:40
-
-
Save yeonghoey/564d65b6b0d3a870944a447bef297450 to your computer and use it in GitHub Desktop.
Normalize dropbox photo names
This file contains 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 os | |
import re | |
# '20161121_001122.jpg' -> '2016-11-21 00.11.22.jpg' | |
TARGET = '%s/Dropbox/Camera Uploads' % os.path.expanduser('~') | |
PATTERN = re.compile(r'^(?P<year>\d\d\d\d)' | |
r'(?P<month>\d\d)' | |
r'(?P<day>\d\d)_' | |
r'(?P<hour>\d\d)' | |
r'(?P<minute>\d\d)' | |
r'(?P<second>\d\d)' | |
r'(?P<trail>.*)$') | |
def format_name(year, month, day, hour, minute, second, trail): | |
return '%(year)s-%(month)s-%(day)s ' \ | |
'%(hour)s.%(minute)s.%(second)s%(trail)s' \ | |
% locals() | |
def rename(a, b): | |
aa = os.path.join(TARGET, a) | |
bb = os.path.join(TARGET, b) | |
print '%s -> %s' % (aa, bb) | |
os.rename(aa, bb) | |
def list_files(): | |
return os.listdir(TARGET) | |
def main(): | |
for filename in list_files(): | |
mo = PATTERN.match(filename) | |
if mo is None: | |
continue | |
a = filename | |
b = format_name(**mo.groupdict()) | |
rename(a, b) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment