批量化修改文件名及后缀
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了批量化修改文件名及后缀相关的知识,希望对你有一定的参考价值。
python小脚本文件,可自主输入某一文件夹,从而修改该文件夹下的文件名和后缀。
修改文件名:人工输入文件名文字部分,批量化由递增数字组成
修改后缀名:人工输入文件后缀,批量化全处理。
代码如下:
# -*- coding: utf-8 -*- """ @author: yinggang zhang change the the file name in a dictionary """ import os def changelastname(all_list_in_adir,readdir): ‘‘‘change the last name in a dictionary‘‘‘ os.chdir(readdir) newlast = input(‘what last name you want ?‘) for subfile in all_list_in_adir: remain = subfile.split(‘.‘)[0] os.renames(subfile,remain+‘.‘+newlast) def changename(all_list_in_adir,readdir): ‘‘‘change the file name in a dictionary‘‘‘ os.chdir(readdir) newname_number = 1 nameword = input(‘please input the name,computer will do the number after the word!‘) for subfile in all_list_in_adir: newname = str(nameword)+‘%d‘ % newname_number remain = subfile.split(‘.‘)[1] os.renames(subfile, newname + ‘.‘ + remain) newname_number += 1 dictionary_place = str(input(‘copy the dictionary path and paste here‘)) all_list = os.listdir(dictionary_place) print(all_list) userchoice = int(input(‘you want to change the file name(1) or the last name(2),type the number 1 or 2:‘)) if userchoice == 1: print(‘you are changing the file name!‘) changename(all_list,dictionary_place) if userchoice == 2: print(‘you are changing the last name!‘) changelastname(all_list,dictionary_place)
新手一枚,并没有处理exception,但基本功能已经做出。希望大家可以批评指正!
可交流python学习心得和经验,以及代码的交流。嘿嘿。
以上是关于批量化修改文件名及后缀的主要内容,如果未能解决你的问题,请参考以下文章