-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit_dir.py
executable file
·39 lines (28 loc) · 1.19 KB
/
split_dir.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python3
# http://stackoverflow.com/questions/12497901/i-need-python-to-process-all-files-in-a-directory-but-it-only-gets-one
# http://www.pythoncentral.io/how-to-slice-listsarrays-and-tuples-in-python/
# https://docs.python.org/2/library/shutil.html
"""Split a large directory into smaller directories"""
import argparse
import os
import shutil
def split_dir(directory, batch_size):
"""Split a large directory into smaller directories"""
files = os.listdir(directory)
size = len(files)
batches = int(size / batch_size)
i = 0
while i <= batches:
os.mkdir(directory + "/" + str(i))
start = i * batch_size
end = i * batch_size + batch_size
part = files[start:end]
for part_file in part:
shutil.move(directory + "/" + part_file, directory + "/" + str(i))
i += 1
if __name__ == "__main__":
PARSER = argparse.ArgumentParser(description="split up a large directory")
PARSER.add_argument("-d", "--directory", help="directory", required=True)
PARSER.add_argument("-n", "--number", help="number", required=True)
ARGS = vars(PARSER.parse_args())
split_dir(ARGS["directory"], int(ARGS["number"]))