-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataspliter.py
71 lines (55 loc) · 2.84 KB
/
dataspliter.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
71
import sys, getopt,os,ntpath, argparse
from pathlib import Path
mainpathList=[]
mainStart=""
curentEnd=""
def splitData(curPath, numberOfSplits,primary):
pathList=[]
userPathList=[]
global mainStart
global curentEnd
global mainpathList
if primary==True:
mainStart=curPath
for user in range(0,numberOfSplits):
pathList.append(curPath+"_"+str(user))
mainpathList.append(curPath+"_"+str(user))
else:
curentEnd=curPath.replace(mainStart,"")
for userPath in mainpathList:
subFolderName=os.path.basename(os.path.normpath(curPath))
pathList.append(userPath+curentEnd)
print (curPath)
onlyfiles = next(os.walk(curPath))[2]
numberOfFilesProUser=int(len(onlyfiles)/len(pathList))
for pathListElement in pathList:
currentNumberOfFiles=numberOfFilesProUser
os.makedirs(pathListElement,511,True)
for Filename in onlyfiles:
if (currentNumberOfFiles>0):
for SameFilename in onlyfiles:
if (currentNumberOfFiles<1 and (Filename.split('.')[0]==SameFilename.split('.')[0]) and (Filename.split('.')[1]!=SameFilename.split('.')[1])) or((Filename.split('.')[0]==SameFilename.split('.')[0]) and (Filename.split('.')[1]!=SameFilename.split('.')[1])):
if Path(curPath+"\\"+Filename).exists():
os.replace(curPath+"\\"+Filename, pathListElement+"\\"+Filename)
onlyfiles = next(os.walk(curPath))[2]
currentNumberOfFiles=currentNumberOfFiles-1
os.replace(curPath+"\\"+SameFilename, pathListElement+"\\"+SameFilename)
onlyfiles = next(os.walk(curPath))[2]
currentNumberOfFiles=currentNumberOfFiles-1
subfolders= [f.path for f in os.scandir(curPath) if f.is_dir()]
if(len(subfolders)>0):
for subPath in list(subfolders):
splitData(subPath, numberOfSplits,False)
parser = argparse.ArgumentParser()
requiredArguments = parser.add_argument_group("required arguments")
requiredArguments.add_argument("-s", "--sourcedir", type=str, required=True, help="Directory containing the annotations and images")
requiredArguments.add_argument("-n", "--numberofsplits", type=int, required=True, help="The number of Splits - for each split a seperate directory will be created")
args = parser.parse_args()
if (not os.path.exists(args.sourcedir) or not os.path.isdir(args.sourcedir)):
sys.exit("Source directory does not exists or is not a directory")
mainPath = args.sourcedir
numberOfSplits = args.numberofsplits
print ('Main path : "', mainPath)
print ('Number of splits : "', numberOfSplits)
splitData(mainPath,numberOfSplits,True)
print ('Done')