#用来分组排序的数组 mySortArr = [] path = r"D:\XXX\视频ts文件夹" for file in os.listdir(path): if file.endswith('.ts'): mySortArr.append(file)
#每个分组最小包含文件数 maxFileNum = 4 deffind_all_index(arr, item): return [i for i, a inenumerate(arr) if a == item] randomListMax = len(mySortArr)*100 defsortA(filelist): tempGroupList = [] #分组 groupList = [] tempGroupLIst = [] tempGroupAppendIndex=0 while tempGroupAppendIndex < randomListMax+1: groupList.append(0) tempGroupAppendIndex += 1 i = 0 countNum = 0 for files in filelist: #遍历所有文件 i = i + 1 countNum +=1 tempGroupList.append(files) randomIndex = random.randint(0,randomListMax) hasPlaceholder = find_all_index(groupList,tempGroupLIst) while (len(hasPlaceholder)!=0): randomIndex = random.randint(0,randomListMax)
if countNum >= maxFileNum : countNum = 0 # print(randomIndex,"randomIndex") rIndex = 0 for tempItem in tempGroupList: rIndex+=1 groupList[randomIndex+rIndex] = tempItem tempItemR = reversed(tempGroupList) # 倒序 for tempItem in tempItemR: groupList[randomIndex+rIndex+len(tempGroupLIst)] = tempItem rIndex+=1 tempItemR = [] tempGroupList = []
removeGroupList = groupList listRes = [] for groupItemOld in removeGroupList: if(groupItemOld!=0): listRes.append(groupItemOld) # random.shuffle(filelist) # print(listRes,"listRes") return listRes #/sort$############################
mySortArr = sortA(mySortArr) # 执行循环遍历 for i inrange(1, loop_times + 1): withopen(f"{i}.txt", 'w', encoding='utf-8') as f: for content in mySortArr: f.write(f"file '{content}'\n") # print(f"{i}.txt 文件生成成功!")