-1

i need to rename a multiple files from a folder with numbers increament in python.i.e. First file in a folder gets renamed with 1 and second file get a name with 2.like that..How to solve this

nisha
  • 69
  • 1
  • 7

1 Answers1

0

You should first iterate through files in a directory, and the rename the files.

It might look like this:

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

This can be the approach and then you should adapt it to your needs.

Community
  • 1
  • 1
mabe02
  • 2,676
  • 2
  • 20
  • 35