Skip to content

Instantly share code, notes, and snippets.

@zupo
Created June 24, 2013 12:56
Show Gist options
  • Select an option

  • Save zupo/5849843 to your computer and use it in GitHub Desktop.

Select an option

Save zupo/5849843 to your computer and use it in GitHub Desktop.

Revisions

  1. zupo revised this gist Jun 24, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion folder_splitter.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    # -*- coding: utf-8 -*-
    # @authore: Peter Lamut
    # @author: Peter Lamut

    import argparse
    import os
  2. zupo created this gist Jun 24, 2013.
    54 changes: 54 additions & 0 deletions folder_splitter.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    # -*- coding: utf-8 -*-
    # @authore: Peter Lamut

    import argparse
    import os
    import shutil

    N = 10 # the number of files in seach subfolder folder


    def move_files(abs_dirname):
    """Move files into subdirectories."""

    files = [os.path.join(abs_dirname, f) for f in os.listdir(abs_dirname)]

    i = 0
    curr_subdir = None

    for f in files:
    # create new subdir if necessary
    if i % N == 0:
    subdir_name = os.path.join(abs_dirname, '{0:03d}'.format(i / N + 1))
    os.mkdir(subdir_name)
    curr_subdir = subdir_name

    # move file to current dir
    f_base = os.path.basename(f)
    shutil.move(f, os.path.join(subdir_name, f_base))
    i += 1


    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')

    return parser.parse_args()


    def main():
    """Module's main entry point (zopectl.command)."""
    args = parse_args()
    src_dir = args.src_dir

    if not os.path.exists(src_dir):
    raise Exception('Directory does not exist ({0}).'.format(src_dir))

    move_files(os.path.abspath(src_dir))


    if __name__ == '__main__':
    main()