How to rename all files in a folder using Python

let’s assume the folder is user_data, relative to the current working directory (where the Python script is in). The code below was tested on Windows.

import os

for filename in os.listdir("./user_data"):
    # print(filename)
    cwd = os.path.dirname(os.path.abspath(__file__)) + '\\user_data\\'
    if filename.split('.')[1] == 'jpg':
        new_filename = filename.split('.')[0] + '_search' + '.jpg'
        print(new_filename)
        os.rename(cwd + filename, cwd + new_filename)