Skip to content

Instantly share code, notes, and snippets.

@jizhilong
Created May 2, 2017 02:41
Show Gist options
  • Save jizhilong/9af5d764c427df992952c52e0c5f44d9 to your computer and use it in GitHub Desktop.
Save jizhilong/9af5d764c427df992952c52e0c5f44d9 to your computer and use it in GitHub Desktop.
import os
import glob
import shutil
def rotate_files(filename, limit):
need_rotate = os.path.exists(filename)
if not need_rotate:
return
files = glob.glob('%s.*' % filename)
files.sort(key=lambda f: int(f.rsplit('.', 1)[1]))
files.insert(0, filename)
num_files = len(files)
if num_files <= limit:
files.append('%s.%d' % (filename, num_files))
start = min(num_files-1, limit-1)
while start >= 0:
src, dest = files[start], files[start+1]
shutil.move(src, dest)
start -= 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment