Skip to content

Instantly share code, notes, and snippets.

@russelldavis
Created October 30, 2012 18:07
Show Gist options
  • Save russelldavis/3981940 to your computer and use it in GitHub Desktop.
Save russelldavis/3981940 to your computer and use it in GitHub Desktop.
Workaround for truncation when writing file timestamps in Python
def copy_mtime(src, dest):
# Don't use os.utime() or shutil.copystat() -- these both truncate the
# time due to python's float not having as much resolution as the
# filesystem -- see http://ciaranm.wordpress.com/2009/11/15/this-week-in-python-stupidity-os-stat-os-utime-and-sub-second-timestamps/
#
# If the truncation during reading (from os.stat()) was the same as the
# truncation during writing, the problem would be transparent here (but
# would still affect other non-truncating code), however python takes the
# problem to a new level and truncates an extra decimal place during write.
# Either way, it's best to avoid the write truncation altogether.
subprocess.check_call(["touch", "-r", src, dest])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment