Skip to content

Instantly share code, notes, and snippets.

@jltemple
Forked from zupo/folder_splitter.py
Last active December 15, 2015 21:59
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 jltemple/bcb83bbcd2d8c94a0d02 to your computer and use it in GitHub Desktop.
Save jltemple/bcb83bbcd2d8c94a0d02 to your computer and use it in GitHub Desktop.
Split a folder with many files into subfolders with N files.Usage: python folder_splitter.py path/to/target/folder
# Modifying folder_splitter.py orginally made by Peter Lamut to move a files into a directory tree based on their last 4 characters
# Similar problem and solves this issue: http://serverfault.com/questions/95444/storing-a-million-images-in-the-filesystem
# EBES06281R0.xml will be moved to /81/r0/EBES06281R0.xml
#
# Original: https://gist.github.com/zupo/5849843
import argparse
import os
import shutil
def move_files(abs_dirname, des_root):
"""Move files into subdirectories."""
for f in os.listdir(abs_dirname):
file_fullpath = os.path.join(abs_dirname, f)
# remove the extension, then lowercase the entire thing
f_base = str.lower(os.path.splitext( f )[0])
des_dir = ""
if len(f_base) >= 4:
# First level directory is the fourth and third to last characters
first_level_dir = f_base[-4:-2]
# Second level directory is the second to last and last characters
second_level_dir = f_base[-2:]
# Join all directories together for full directory path
des_dir = os.path.join(des_root, first_level_dir, second_level_dir)
else:
des_dir = os.path.join(des_root, "other")
# create new subdir if necessary
if not os.path.exists(des_dir):
# makedirs will recursively make an intermediate-level directories as well
os.makedirs(des_dir)
# COPY the file to its proper directory
# copy2() copies metadata (cp -p), copy() will not
shutil.copy2(file_fullpath, des_dir)
# MOVE the file to its proper directory
# shutil.move(file_fullpath, des_dir)
def parse_args():
"""Parse command line arguments passed to script invocation."""
parser = argparse.ArgumentParser(
description='Split files into multiple subfolders.')
parser.add_argument('src_dir', help='source directory')
parser.add_argument('des_root', help='directory to create the directory tree and move the files to')
return parser.parse_args()
def main():
"""Module's main entry point (zopectl.command)."""
args = parse_args()
src_dir = args.src_dir
des_root = args.des_root
if not os.path.exists(src_dir):
raise Exception('Directory does not exist ({0}).'.format(src_dir))
move_files(os.path.abspath(src_dir), os.path.abspath(des_root))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment