Skip to content

Instantly share code, notes, and snippets.

@priyadarshan
Created May 27, 2013 20:25
Show Gist options
  • Save priyadarshan/5658917 to your computer and use it in GitHub Desktop.
Save priyadarshan/5658917 to your computer and use it in GitHub Desktop.
Reorganise Library
import os
import re
import subprocess
import sys
PART_RE = re.compile(ur"^(.+)-part-\d+$")
if __name__ == "__main__":
root = os.path.expanduser(sys.argv[1])
cwd = os.getcwdu()
os.chdir(root)
bookDirs = [d for d in os.listdir(root) if not d.startswith(".")]
for bookDir in bookDirs:
m = PART_RE.match(bookDir)
if m:
baseName = m.group(1)
basePath = os.path.join(root, baseName)
if not os.path.isdir(basePath):
os.mkdir(basePath)
bookPath = os.path.join(root, bookDir)
if os.path.exists(bookPath):
txtPath = os.path.join(bookPath, bookDir + ".txt")
if os.path.exists(txtPath):
filename = bookDir + ".txt"
txtPath = os.path.join(bookDir, filename)
subprocess.call(["git", "mv", txtPath, baseName])
print filename
os.rmdir(bookPath)
os.chdir(cwd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment