我需要在python中使用数字增量重命名文件夹中的多个文件

时间:2016-01-21 08:45:08

标签: python

我需要重命名python.i.e中数字增量的文件夹中的多个文件。文件夹中的第一个文件被重命名为1,第二个文件获得了一个名称2.like that..How to solve this

1 个答案:

答案 0 :(得分:0)

您应首先 iterate through files in a directory rename 文件。

可能看起来像这样:

import os

# Let's say you want to rename files in the current directory
index = 1
for filename in os.listdir(os.getcwd()):
    os.rename(filename, '_'.join([str(index),filename]))
    index += 1

这可以是方法,然后您应该根据您的需要进行调整。