如何在python中重命名文本文件?

如何在python中重命名文本文件?,python,python-3.x,text-processing,python-textprocessing,Python,Python 3.x,Text Processing,Python Textprocessing,我有100个文本文件,我想按顺序重命名文件 my text files looks like this note 1.txt, note 6.txt,note 15.txt,note 24.txt 使用os和re模块 Ex: import os import re path = "Your Path" for num, file in enumerate(sorted(os.listdir(path), key=lambda x: int(re.search(r"(\d+)", x).g

我有100个文本文件,我想按顺序重命名文件

my text files looks like this 

note 1.txt, note 6.txt,note 15.txt,note 24.txt

使用
os
re
模块

Ex:

import os
import re
path = "Your Path"

for num, file in enumerate(sorted(os.listdir(path), key=lambda x: int(re.search(r"(\d+)", x).group(1))), 1):
    if not re.search(r"\b{}\.txt".format(num), file):
        print("Rename", file, num)
        os.rename(os.path.join(path, file), os.path.join(path, re.sub(r"\d+", str(num), file)))
    else:
        print("Good", file, num)

什么代码试图重命名这些文件@Krishna@MonisMajeed我对snames:s_num=os.path.splitext(file)[0].split()[1]lst_num=[1,6,8,12,25,28,41,47,71,72,84,86,90,94,97]中的文件使用了相同的代码
code snames=glob(“Note*.txt”),如果lst_num中的int(s_num):对于lst_num中的i:indx=lst_num.index(i)++1 print(indx)dst=“Note”+str(i)+“.txt”src=文件os.rename(src,dst)
i'm facing an issue like this 

Cannot create a file when that file already exists: 'note 1.txt' -> 'note 6.txt'

it should come in order  of 1,2,3....
import os
import re
path = "Your Path"

for num, file in enumerate(sorted(os.listdir(path), key=lambda x: int(re.search(r"(\d+)", x).group(1))), 1):
    if not re.search(r"\b{}\.txt".format(num), file):
        print("Rename", file, num)
        os.rename(os.path.join(path, file), os.path.join(path, re.sub(r"\d+", str(num), file)))
    else:
        print("Good", file, num)