forked from HunterCQu/AITools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
files2folders.py
70 lines (55 loc) · 1.89 KB
/
files2folders.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# -*- coding: utf-8 -*-
# Author : Andy Liu
# Last modified: 2018-09-20
# This tool is used to pick some files into some folders
# input: python files2folders.py /home/andy/data/image_folder /home/andy/data/result_folder
# output:
# ./result_folder
import os
import shutil
import sys
from tqdm import tqdm
import argparse
carNO = "re01"
date = "20180918"
cameraNO = "01"
per_num = 500
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('srcFolder', help='Source directory', type=str)
parser.add_argument('dstFolder', help='Destination directory', type=str)
args = parser.parse_args()
return args
def pick(srcFolder, dstFolder):
num = 1 # folder NO
if srcFolder[-1] == "/":
srcFolder = srcFolder[:-1]
if dstFolder[-1] == "/":
dstFolder = dstFolder[:-1]
dstpath = dstFolder + "/" + carNO + "_"+ date + "_" + cameraNO + "_" + str(num).zfill(2)
# recreate folder
if os.path.exists(dstpath):
shutil.rmtree(dstpath)
os.makedirs(dstpath)
imglist = os.listdir(srcFolder)
imgnum = len(imglist)
for i in tqdm(range(imgnum)):
if i%per_num == 0 and i !=0:
num += 1
dstpath = dstFolder + "/" + carNO + "_"+ date + "_" + cameraNO + "_" + str(num).zfill(2)
if os.path.exists(dstpath):
shutil.rmtree(dstpath)
os.makedirs(dstpath)
shutil.copyfile(srcFolder + "/" + imglist[i], dstpath + "/" + imglist[i])
if __name__ == '__main__':
args = parse_args()
srcFolder = args.srcFolder
dstFolder = args.dstFolder
if not os.path.exists(srcFolder):
print("Error !!! %s is not exists, please check the parameter"%srcFolder)
sys.exit(0)
if not os.path.exists(dstFolder):
print("Error !!! %s is not exists, please check the parameter"%dstFolder)
sys.exit(0)
pick(srcFolder, dstFolder)
print("Done!")