SoFunction
Updated on 2025-05-13

Python implements file batch name change function complete example code

I have recorded a set of courses, because sometimes some subsections need to be inserted in the middle, I hope to automatically batch modify the file name.

for example

#Project creation.mp402.Variables and types.mp4
03.More data types.mp4
[email protected]
04.Arithmetic operators.mp4
05.Relational operators.mp4

Will become

#Project creation.mp402.Variables and types.mp4
03.More data types.mp4
.mp4
05.Arithmetic operators.mp4
06.Relational operators.mp4

There is one thing to note below: the number of files exceeds 100, and zfill(2) should be changed to zfill(3)

import os

folder_path = 'e:/xxx course recording 20250402back'
txt_files = [f for f in (folder_path) if ('.mp4')]
txt_files.sort()  # Sort by file name
# Countercount = 1

for filename in txt_files:
    print(filename)
    arr = ('.')
    newfilename = '.'.join(arr[1:])
    # print(newfilename)
    # Construct a new file name    new_name = f"{str(count).zfill(2)}.{newfilename}"
    print(new_name)

    #
    # # The complete path of the original file    old_path = (folder_path, filename)
    # # The complete path to the new file    new_path = (folder_path, new_name)
    #
    # # Rename the file    (old_path, new_path)
    print(f"Rename: {filename} -> {new_name}")
    #
    count += 1

Summarize

This is the article about Python's implementation of batch name renaming function. For more related content on batch name renaming of Python files, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!