Skip to content

Instantly share code, notes, and snippets.

@tommct
Created November 6, 2015 00:17
Show Gist options
  • Save tommct/f5fcfea7d3977f1cdfd2 to your computer and use it in GitHub Desktop.
Save tommct/f5fcfea7d3977f1cdfd2 to your computer and use it in GitHub Desktop.
Change Modification Date via Python

This is Python code for updating the file modification date of a file on MacOSX or Linux. In this example, I had copied .dv files from my camcorder, which encoded the date in the filename, but had as the modification date, the time I transferred the file from the camcorder.

import os
import time
fpath = '/path/to/dv/files'
for root, dirs, files in os.walk(fpath):
    for name in files:
        if name[-3:]=='.dv':
            try:
                t = time.mktime(time.strptime(name, 'clip-%Y-%m-%d %H;%M;%S.dv'))
                os.utime(os.path.join(root,name), (t,t))
            except ValueError as err:
                print(root,name)
                print(err)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment