Modify the file format in batches (python code + exe file).

Python batch modify file format

running result:

insert image description here
First, enter the path of the folder, and press Enter after entering.
After that, select the corresponding format to be modified.

code part:


# -*- coding: utf-8 -*-
import os
#设定文件路径
path=input('请输入文件夹路径(图片所存放的文件夹路径,不是文件):')
print('支持转换格式列表:')
print('-----1 jpg-----')
print('-----2 png-----')
print('-----3 bmp-----')
mod=int(input('请输入要转换成什么格式(输入数字序号):'))
#path='C:\\Users\\86159\\Desktop\\PLD\\PLDM\\test_gt'#在此处输入文件路径
i=1
#对目录下的文件进行遍历
for file in os.listdir(path):
#判断是否是文件
    if os.path.isfile(os.path.join(path,file))==True:
#设置新文件名
        if mod == 1:
            new_name=file.replace(file,"%d.jpg"%i) #以.jpg为例
        if mod == 2:
            new_name=file.replace(file,"%d.png"%i) #以.jpg为例
        if mod == 3:
            new_name=file.replace(file,"%d.bmp"%i) #以.jpg为例
#重命名
        os.rename(os.path.join(path,file),os.path.join(path,new_name))
        i+=1
#结束
print ("End")


Executable program

For the convenience of some friends who do not want to install Python. The blogger exported the modified program as an exe executable file.
The effect is as follows:
insert image description here
This operation saves the trouble of installing the os library, which is convenient for most office workers.
You can go to: Click here to download the exe file directly.

Guess you like

Origin blog.csdn.net/Deng7326/article/details/127796647