python 批量重命名'數字.ts'文件(精簡版)

ps:由於cmd合併ts文件時按照文件名順序,造成合成視頻無法播放,所以文件需要從小到大排序。
文件名本身爲純數字,只要指定字符串長度就可以實現文件按大小順序排列了。

運行環境:win10 ,Python3.6

編輯器:sublime

運行:cmd

模塊:os

copyright©Kevin.G


import os
path=input('')
filelist=os.listdir(path)
for file in filelist:
	oldname=os.path.join(path,file)
	filename=os.path.splitext(file)[0]
	filetype=os.path.splitext(file)[1]
	s_width=filename.zfill(4)
	newname=os.path.join(path,s_width+filetype)
	os.rename(oldname,newname)
	# 出現三次運行成功,原文件並沒有變化,因爲os.rename()寫錯:
	# 1.os.name()
	# 2.os.rename=()
	print(oldname,'....',newname)

運行前:

運行結果:


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