Make bulk changes using the Python file suffix

Make bulk changes using the Python file suffix

Code

import os

files = os.listdir('.')
for file_name in files:
    portion = os.path.splitext(file_name)
    if portion[1] == ".jpg":
        new_name = portion[0] + ".gif"
        os.rename(file_name, new_name)

The above code is the suffix "jpg" files into the suffix "gif" files.

Instructions

The python file containing the code above change.pycopies you want to change the file extension to a folder, open the cmd command line, enter the following command.

python change.py

Guess you like

Origin www.cnblogs.com/banshaohuan/p/11419935.html