Amazing-Python-Scripts

Форк
0
50 строк · 1.3 Кб
1
import os
2
from math import log10
3

4
files = os.listdir(".")
5

6
files.sort(key=lambda x: os.path.getmtime(x))
7
# print(files) # list index 0 hold the oldest file
8
digits = int(log10(len(files)))+1
9

10
print(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")
11
n = input()
12

13
if (n == "y" or n == "Y" or n == "p"):
14
    rename, hidden = 0, 0
15
    for i in range(len(files)-1):
16

17
        oldname = files[i]
18

19
        if (files[i][0] == "."):
20
            hidden += 1
21
            continue
22

23
        if "_" in files[i] and files[i][0] != "_":
24
            index = files[i].find("_")
25
            isNum = files[index-1]
26

27
            if (isNum >= "0" and isNum <= "9"):
28
                tempOld = files[i][index+1:]
29
            else:
30
                tempOld = files[i]
31
        else:
32
            tempOld = files[i]
33

34
        rename += 1
35
        newfile_name = f"{str(rename).zfill(digits)}_{tempOld}"
36

37
        if (n == "p"):
38
            print(f"{tempOld} --> {newfile_name}")
39
        else:
40
            os.rename(oldname, newfile_name)
41

42
    print(
43
        f"\nNumber of hidden files are {hidden}. For safety purpose, they will be kept unchanged.")
44

45
else:
46
    print("File renaming cancel.")
47
    exit(0)
48

49
if n == "y" or n == "Y":
50
    print("\nFile renaming successful.")
51

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.