Python 如何重命名文件夹中的文件列表

Python 如何重命名文件夹中的文件列表,python,python-3.x,Python,Python 3.x,我在一个文件夹中设置了100个文件。我需要重命名这些文件。我的输入文件如下 1) 0000000001.001 2) 0000000001.002 3) 0000000001.003 4) 0000000002.001 5) 0000000002.002 6) 0000000002.003 7) 0000000003.001 8) 0000000003.002 9) 0000000003.003 我试过这个: import os folder ="C:/Users/xyz"

我在一个文件夹中设置了100个文件。我需要重命名这些文件。我的输入文件如下

 1) 0000000001.001
 2) 0000000001.002
 3) 0000000001.003
 4) 0000000002.001
 5) 0000000002.002
 6) 0000000002.003
 7) 0000000003.001
 8) 0000000003.002
 9) 0000000003.003
我试过这个:

import os

folder ="C:/Users/xyz"

rename_dict = {'001': '1', '002': '2', '005': '5'}
for filename in os.listdir(folder):
    base_file, ext = os.path.splitext(filename)
    ext = ext.replace('.','')
    if ext in rename_dict:
        new_ext = rename_dict[ext]
        new_file = base_file + new_ext
        old_path = os.path.join(folder, filename)
        new_path = os.path.join(folder, new_file)
        os.rename(old_path, new_path)
例外输出:

 1) 0000000101.1
 2) 0000000101.2
 3) 0000000101.3
 4) 0000000102.1
 5) 0000000102.2
 6) 0000000102.3
 7) 0000000103.1
 8) 0000000103.2
 9) 0000000103.3
我需要以增量方式重命名文件名。

给你(假设它们是.txt文件)

给你(假设它们是.txt文件)


电流输出是什么?电流输出是什么?
import os
folder ="C:\\Users\\xyz\\"
os.chdir(folder)
for filename in os.listdir(folder):
    newName = filename.split(".")[0] + "." +  filename.split(".")[1][-1:] + '.txt'   
    print newName
    os.rename(filename, newName)