Renaming a file sequence

Try this

import os, os.path

# simple version for working with CWD
#print (len([name for name in os.listdir('.') if os.path.isfile(name)]))

# path joining version for other paths
DIR = r"folder"
a=print (len([name for name in os.listdir(DIR) if os.path.isfile(os.path.join(DIR, name))]))
print(a)
# Python program to rename all file 
# names in your directory 


os.chdir('folder2') 
print(os.getcwd()) 
COUNT = a

# Function to increment count 
# to make the files sorted. 
def increment(): 
    global COUNT 
    COUNT = COUNT + 1


for f in os.listdir(): 
    f_name, f_ext = os.path.splitext(f) 
    f_name = "foldername" + str(COUNT) 
    increment() 

    new_name="{} {}".format(f_name, f_ext) 
    os.rename(f, new_name) 

Leave a Comment