rename all files in a folder in python

To rename all files in a folder using Python, you can use the os module along with the os.rename() function. Here's an example:

main.py
import os

# Specify the folder path
folder_path = '/path/to/folder'

# Get all the file names in the folder
file_names = os.listdir(folder_path)

# Iterate over the files and rename each one
for file_name in file_names:
    # Construct the new file name
    new_file_name = 'new_' + file_name

    # Rename the file
    os.rename(os.path.join(folder_path, file_name), os.path.join(folder_path, new_file_name))
411 chars
16 lines

In this example, folder_path should be replaced with the actual path of the folder where the files are located. The new names are constructed by adding a prefix or any other desired modification to the original file names.

Note: Make sure that the folder path is correct and that you have the necessary permissions to rename the files.

related categories

gistlibby LogSnag