码迷,mamicode.com
首页 > 其他好文 > 详细

批量化修改文件名及后缀

时间:2016-09-03 19:53:14      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

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学习心得和经验,以及代码的交流。嘿嘿。

批量化修改文件名及后缀

标签:

原文地址:http://www.cnblogs.com/yinggangzhang/p/5837707.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!