-
-
Save zupo/5849843 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*- | |
# @author: 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() |
This just spared me from creating 50000 folders manually and moving each image into them separately. Real life saver! Thanks awfully!
If anyone has sequential images that you want to keep order, replace line 14 with:
files = sorted([os.path.join(abs_dirname, f) for f in os.listdir(abs_dirname)])
With python3, I got error
subdir_name = os.path.join(abs_dirname, '{0:03d}'.format(i / N + 1)) ValueError: Unknown format code 'd' for object of type 'float'
Fixed by updating line 22:
subdir_name = os.path.join(abs_dirname, '{0:03d}'.format(i // N + 1))
Thanks @hyzyla ... This edit worked for me in python3.8
Thanks @zupo ... I was searching for the exact script ...
Thanks
This is awesome. My requirement is exactly similar to this. I want to move upto 200 files into each folder but with file start characters by nesting it.
For example,
If filename starts with 'absolute.txt', 'ab.txt', 'bingo.txt' it would be moved to "a" folder after that inside "a" folder, if files are more than 200 it need to create another folder inside "a" as "aa",ab" and so on, after that inside "aa" if there is more than 200 files inside "aa" folder it would need to create "aaa","aab" and so on.
This will make organize easy to get to required file
I posted this snippet years back and people still use it, can't believe it. 😄
In any case, if you are on MacOS, checkout my new thing: https://paretosecurity.app/
running 'python split.py' gave me the following error usage: split.py [-h] src_dir split.py: error: the following arguments are required: src_dir
how do i fix this? please share exact code lines to fix this. i'm new to this.
It tells u it needs the scr_dir as argument
running 'python split.py' gave me the following error usage: split.py [-h] src_dir split.py: error: the following arguments are required: src_dir
how do i fix this? please share exact code lines to fix this. i'm new to this.It tells u it needs the scr_dir as argument
- click on the RAW button at the top of this code window.
- copy, paste the code, and save the file as 'folder_splitter.py'
Usage:
3. Suppose
a. you want to reorganize a folder called MESSY
b. the path to MESSY is F:\MESSY
-
copy paste 'folder_splitter.py' to F:\
-
open command prompt
-
in command prompt,
a. move cursor to F:
b. type the following code
python folder_splitter.py F:\MESSY
Thanks, very useful.
Had 29,000 batch-downloaded Sporepedia creations and found that Spore had a limit to how many files could be dropped in at a time. This saved so much time
It works perfectly, thanks, I needed to split a folder with +450000 French epubs.