Skip to content

Instantly share code, notes, and snippets.

@yeonghoey
Created November 21, 2016 22:40
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 yeonghoey/564d65b6b0d3a870944a447bef297450 to your computer and use it in GitHub Desktop.
Save yeonghoey/564d65b6b0d3a870944a447bef297450 to your computer and use it in GitHub Desktop.
Normalize dropbox photo names
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