Skip to content

Instantly share code, notes, and snippets.

@zhwei
Created February 9, 2015 03:01
Show Gist options
  • Save zhwei/d1d10c7287fbf45928d9 to your computer and use it in GitHub Desktop.
Save zhwei/d1d10c7287fbf45928d9 to your computer and use it in GitHub Desktop.
rename file script
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Rename: baixing.log-1420614280.gz => baixing.log-20150107-150440.gz
import os
import datetime
to_date = lambda x: datetime.datetime.fromtimestamp(int(x)).strftime('%Y%m%d-%H%M%S')
file_list = os.listdir(".")
for fi in file_list:
if fi.count("-") != 1 or fi.count(".gz") < 1:
continue
prefix, postfix = fi.split("-")
new_date = to_date(postfix.split(".gz")[0])
filename = prefix + "-" + new_date + ".gz"
os.rename(fi, filename)
print(fi, "=>", filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment