Skip to content

Instantly share code, notes, and snippets.

@westscz
Created May 26, 2018 08:30
Show Gist options
  • Save westscz/cb670a676170111ef4d3ab035b0e1403 to your computer and use it in GitHub Desktop.
Save westscz/cb670a676170111ef4d3ab035b0e1403 to your computer and use it in GitHub Desktop.
Change filenames for Xiaomi 3S files to format 'yyyy-mm-dd hh-mm-ss'
"""
Change filenames for Xiaomi 3S files to format 'yyyy-mm-dd hh-mm-ss'
"""
import re
import os
def get_new_filename(filename):
result = re.search("\w*_(\d{4})(\d{2})(\d{2})_(\d{2})(\d{2})(\d{2})(?:_\w*?)\.(\w*)", filename)
if result:
year, month, day, hour, minute, second, f = result.groups()
return "-".join([year, month, day]) + ' ' + ".".join([hour, minute, second, f])
else:
return ""
if __name__ == '__main__':
directory = ""
dir_files = os.listdir(directory)
for file in dir_files:
new_file = get_new_filename(file)
if new_file:
os.rename(os.path.join(directory, file), os.path.join(directory, new_file))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment