Amazing-Python-Scripts
50 строк · 1.3 Кб
1import os2from math import log103
4files = os.listdir(".")5
6files.sort(key=lambda x: os.path.getmtime(x))7# print(files) # list index 0 hold the oldest file
8digits = int(log10(len(files)))+19
10print(f"\nPlease verify the new file names for accuracy: press 'p' to review the file names, or 'y'/'Y' to proceed with the renaming process. Press any other key to exit the program.\n")11n = input()12
13if (n == "y" or n == "Y" or n == "p"):14rename, hidden = 0, 015for i in range(len(files)-1):16
17oldname = files[i]18
19if (files[i][0] == "."):20hidden += 121continue22
23if "_" in files[i] and files[i][0] != "_":24index = files[i].find("_")25isNum = files[index-1]26
27if (isNum >= "0" and isNum <= "9"):28tempOld = files[i][index+1:]29else:30tempOld = files[i]31else:32tempOld = files[i]33
34rename += 135newfile_name = f"{str(rename).zfill(digits)}_{tempOld}"36
37if (n == "p"):38print(f"{tempOld} --> {newfile_name}")39else:40os.rename(oldname, newfile_name)41
42print(43f"\nNumber of hidden files are {hidden}. For safety purpose, they will be kept unchanged.")44
45else:46print("File renaming cancel.")47exit(0)48
49if n == "y" or n == "Y":50print("\nFile renaming successful.")51