python 數據處理2----如何批量對數據進行打亂並重命名

可以通過增加多位隨機數字,重複執行以下python 代碼代碼,

然後在執行之前分享的python數據處理1--對批量數據重名

https://blog.csdn.net/u012514944/article/details/89705504

就可以做到了!

	for category in os.listdir(dirname):
		print(category)
		catdir = os.path.join(dirname,category)
		if not os.path.isdir(catdir):
			continue
		files = os.listdir(catdir)
		print(files)
		#count = startcount
		fisrt = 1;
		for cur_file in files:
			print("正在處理" + category + "分類下的" + cur_file)
			filename = os.path.join(catdir,cur_file)
			#count = count + 1
			second = random.randint(0,9)
			third = random.randint(0,9)
			fourth = random.randint(0,9)
			fifth = random.randint(0,9)
			sixth = random.randint(0,9)
			t = time.time()
			seventh = int(round(t*1000))
			#原來的文件路徑
			oldDir = os.path.join(catdir,cur_file)
			if os.path.isdir(oldDir):
				continue
			filename = os.path.splitext(cur_file)[0]
			filetype = os.path.splitext(cur_file)[1]
			newDir = os.path.join(catdir,str(fisrt)+str(second)+str(third)+str(fourth)+str(fifth)+str(sixth)+str(seventh)+filetype)
			os.rename(oldDir,newDir)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章