Created
February 25, 2019 00:55
-
-
Save jahentao/e5fdf363a932a9039a3d2c29c037bbd4 to your computer and use it in GitHub Desktop.
Python按时间倒序排序文件夹内文件 #Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
# 按修改时间排序后,批量重命名文件,并加序号前缀 | |
def renameFilesSortedByTime(dirPath): | |
mlist = [] | |
# 获得文件名列表 | |
files = os.listdir(dirPath) | |
# 获得名称带时间戳的新文件名列表 | |
for filename in files: | |
# 获得文件的最后修改时间 | |
createTime = os.path.getmtime(dirPath + filename) | |
# 将最后修改时间戳作为文件名的前缀,得到新的文件名,加入列表 | |
mlist.append(str(int(createTime)) + "-" + filename) | |
# 重新给列表排序,这次所有文件按修改时间排序了 | |
mlist = sorted(mlist, reverse=True) | |
# 遍历修改时间戳为序号 | |
for i in range(len(mlist)): | |
# 截取获得原先的文件名 | |
oldName = mlist[i][11:] | |
# 将时间戳部分修改为序号,得到新的文件名 | |
newName = mlist[i][11:] | |
if (i + 1) < 10: | |
newName = "00" + str(i + 1) + newName | |
elif (i + 1) > 9 and (i + 1) < 100: | |
newName = "0" + str(i + 1) + newName | |
else: | |
newName = str(i + 1) + newName | |
# print(newName, oldName) | |
# 重命名文件,按修改时间排序并加序号前缀 | |
os.rename(dirPath + oldName, dirPath + newName) | |
if __name__ == '__main__': | |
renameFilesSortedByTime("D:/jahentao/Documents/Courses/极客时间/深入剖析Kubernetes/geektime/主课程/") | |
print("Job Done!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment