Skip to content

Instantly share code, notes, and snippets.

@Exodus111
Created February 1, 2014 06:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Exodus111/8748644 to your computer and use it in GitHub Desktop.
Save Exodus111/8748644 to your computer and use it in GitHub Desktop.
"""
Folderizer.
This is a program to sort downloaded series files into individual folders.
This program does in no way condone illegal file downloading.
Downloading files illegally is immoral and WRONG, and can be punished by up to life in prison or the death penalty (depending on your state or country).
Illegal downloads steals the hard earned money of not only starving artists but most importantly hard working executive producers, who desire nothing but the well being of their audience, but thanks to illegal downloading, can barely survive in todays economy living only by sleeping on their friends couches and hitchhiking to work.
Use this program at your own discretion, I am in no way responsible for what you do with it, or how you chose to live your life, but call your mom dude... seriously.
This program is Open Source (GPLv3 license).
"""
import argparse
import re
from path import path
root = path("./Test")
#This function finds the correct files or folders following the SxxExx pattern.
def _find_pattern(names):
pattern = "S\d+E\d"
checked = {}
for i in names:
num = re.search(pattern, i.basename())
if num != None:
checked[i] = num.start()
return checked
#Here we empty and delete all relevant folders.
def flatten(root):
folders = _find_pattern(root.dirs())
for folder in folders:
for files in folder.walkfiles():
files = _find_pattern([files])
for file in files:
path(file).move(root)
folder.rmtree_p()
#Here we make our new folders and move all files from root to where they belong.
def sort(root):
vidfiles = _find_pattern(root.files())
for file in vidfiles:
filename = str(file.basename())
filename = filename[:vidfiles[file]].rstrip(".")
filename = filename.replace(".", " ")
folder = root / path(filename)
folder.mkdir_p()
file.move(folder)
#Argparse allows the user to input commands to our program in the CLI.
parser = argparse.ArgumentParser(prog="folderizer", description="Find all episode files and put them into their own folders. Requires the flatten argument to be run first then sort. The program looks for folders and files with the pattern SxxExx (Ex: S01E03), and will ignore all other files.")
parser.add_argument("-f", "--flatten", help="Check all subdirectories and move the relevant files to the root directory. Will delete all relevant subdirectories", action="store_true")
parser.add_argument("-s", "--sort", help="Sort all the files in the root directory, making and moving the relevant files to their own folders", action="store_true")
args = parser.parse_args()
if args.flatten:
flatten(root)
elif args.sort:
sort(root)
else:
print "Please pick an option: --flatten, --sort"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment