|
| 1 | +import glob |
| 2 | +import os |
| 3 | +from shutil import copy2 |
| 4 | +import sys |
| 5 | + |
| 6 | + |
| 7 | +def get_files(path): |
| 8 | + ''' |
| 9 | + return a list of files avialable in given folder |
| 10 | + ''' |
| 11 | + files = glob.glob(f'{path}/*') |
| 12 | + return files |
| 13 | + |
| 14 | + |
| 15 | +def getfullpath(path): |
| 16 | + ''' |
| 17 | + Return absolute path of given file |
| 18 | + ''' |
| 19 | + return os.path.abspath(path) |
| 20 | + |
| 21 | + |
| 22 | +def copyfiles(src, dst): |
| 23 | + ''' |
| 24 | + This function copy file from src to dst |
| 25 | + if dst dir is not there it will create new |
| 26 | + ''' |
| 27 | + if not os.path.isdir(dst): |
| 28 | + os.makedirs(dst) |
| 29 | + copy2(src, dst) |
| 30 | + |
| 31 | + |
| 32 | +def split(data, count): |
| 33 | + ''' |
| 34 | + Split Given list of files and return generator |
| 35 | + ''' |
| 36 | + for i in range(1, len(data), count): |
| 37 | + if i + count-1 > len(data): |
| 38 | + start, end = (i-1, len(data)) |
| 39 | + else: |
| 40 | + start, end = (i-1, i+count-1) |
| 41 | + yield data[start:end] |
| 42 | + |
| 43 | + |
| 44 | +def start_process(path, count): |
| 45 | + files = get_files(path) |
| 46 | + splited_data = split(files, count) |
| 47 | + |
| 48 | + for idx, folder in enumerate(splited_data): |
| 49 | + name = f'data_{idx}' |
| 50 | + for file in folder: |
| 51 | + copyfiles(getfullpath(file), getfullpath(name)) |
| 52 | + |
| 53 | + |
| 54 | +if __name__ == "__main__": |
| 55 | + ''' |
| 56 | + driver code |
| 57 | + To run this script |
| 58 | + python split_and_copy.py <input folder path> <20> |
| 59 | + ''' |
| 60 | + |
| 61 | + if len(sys.argv) != 3: |
| 62 | + print("Please provide correct parameters \ |
| 63 | + \npython split_and_copy.py <input folder path> <count>") |
| 64 | + sys.exit(0) |
| 65 | + |
| 66 | + if len(sys.argv) == 3: |
| 67 | + path = sys.argv[1] |
| 68 | + if os.path.isdir(path): |
| 69 | + count = sys.argv[2] |
| 70 | + start_process(path, int(count)) |
| 71 | + else: |
| 72 | + print('Given directory name is not an valid directory') |
| 73 | + else: |
| 74 | + print('Wrong paramter are provided') |
0 commit comments