您的位置:首页 > 编程语言 > Python开发

Python小脚本—批量修改文件名与文件扩展名升级版

2016-05-27 22:32 555 查看

0x00:前言

这次又碰到了要修改一个目录下大量文件的名字的问题,比如将文件名字中的所有”hack”字符串都替换为”hacker”字符串,如果目录下还有很多子目录,一个个手工改的话且不是太浪费时间,而且还很烦。我第一次写的

Python小脚本—批量修改文件名与文件扩展名 只支持当前主目录下文件名和文件后缀名的修改,不支持子目录下文件的修改和子目录名的修改,而且用起来还要改源码,十分不方便,于是就有了脚本升级版。

0x01 :说明

主要功能:

替换指定目录下的所有子目录和文件名字的指定字符串,相当于批量改名;
替换掉指定目录下(包括其子目录下)所有文件的后缀名中的指定字符串,相当于批量修改文件后缀名。


用法:

[+] 用法: python Modifer.py  [指定目录] [选项] [参数1] [参数2]
[+] 选项 [-fp]  :批量修改文件后缀名                 [参数1]: 原后缀名             [参数2]:替换的后缀名
[+] 选项 [-fd]  :批量修改目录和文件名               [参数1]: 需要替换的字符        [参数2]:替换字符串
[+] 选项 [-all] :批量修改目录、文件名和文件后缀名     [参数1]: 需要替换的字符部分    [参数2]:替换字符串
[+] 用法示例:python Modifer.py D:\files -fp txt data")


使用-fd选项时,不仅文件名字中的指定字符串”str”会被替换掉,如果子目录也含有字符串”str”,则子目录名字中的”str”也会被替换掉。

使用截图:



0x02:源码与下载地址

下载地址: Modifer

源码

# coding:utf-8
# Build by LandGrey 2016-05-27

import os
import sys

#批量修改路径与文件名
def Modifyprefix(Path,oldcontent,newcontent):
all_file_list = os.listdir(Path)                            #列出指定目录下的所有文件
for file_name in all_file_list:
currentdir =os.path.join(Path, file_name)               #连接指定的路径和文件名or文件夹名字
if os.path.isdir(currentdir):                           #如果当前路径是文件夹,则跳过
Modifyprefix(currentdir,oldcontent,newcontent)
fname = os.path.splitext(file_name)[0]                  #分解出当前的文件路径名字
ftype = os.path.splitext(file_name)[1]                  #分解出当前的文件扩展名
if oldcontent in fname:
fdcount[0]+=1
replname =fname.replace(oldcontent,newcontent)      #将原文件名中的'oldcontent'字符串内容全替换为'newcontent'字符串内容
newname = os.path.join(Path,replname+ftype)         #文件路径与新的文件名字+原来的扩展名
os.rename(currentdir,newname)                       #重命名

#批量修改文件扩展名(后缀)
def Modifypostfix(Path,oldftype,newftype):
all_file_list = os.listdir(Path)          #列出指定目录下的所有文件
for file_name in all_file_list:
currentdir =os.path.join(Path,file_name)
if os.path.isdir(currentdir):                    #迭代
Modifypostfix(currentdir,oldftype,newftype)
fname = os.path.splitext(file_name)[0]
ftype = os.path.splitext(file_name)[1]
if oldftype in ftype[1:]:                         #找到需要修改的扩展名
typecount[0]+=1
ftype=ftype.replace(oldftype,newftype)
newname = os.path.join(Path,fname+ftype) #文件路径与原来的文件名字+新的扩展名
os.rename(currentdir,newname)               #重命名

def Useage():
print unicode("\n[+] 用法: python Modifer.py  [指定目录] [选项] [参数1] [参数2]","utf-8")
print unicode("[+] 选项 [-fd]  :批量修改目录和文件名               [参数1]: 需要替换的字符      [参数2]:替换字符串","utf-8")
print unicode("[+] 选项 [-fp]  :批量修改文件后缀名                 [参数1]: 原后缀名            [参数2]:需要替换的后缀名","utf-8")
print unicode("[+] 选项 [-all] :批量修改目录、文件名和文件后缀名   [参数1]: 需要替换的字符部分  [参数2]:替换字符串","utf-8")
print unicode(r"[+] 用法示例:python Modifer.py D:\files -fp txt data","utf-8")

if __name__=="__main__":
typecount=[0]
fdcount=[0]
if len(sys.argv)==2 and "-h" in sys.argv[1]:
Useage()
sys.exit()
elif len(sys.argv) !=5:
print unicode("\n[+] 参数错误 !\n","utf-8")
print unicode("[+] 用 -h 或--help 参数查看Modifer.py用法","utf-8")
sys.exit()
elif os.path.isdir(sys.argv[1]) is False:
print unicode("\n[+] 指定目录错误 ! 请检查输入路径是否正确,路径中不能有空格\n","utf-8")
print unicode("[+] 用 -h 或--help 参数查看Modifer.py用法","utf-8")
sys.exit()
elif sys.argv[2]=="-fd":
Modifyprefix(sys.argv[1],sys.argv[3],sys.argv[4])
print unicode("\n[+] Modifer.py    Build by LandGrey","utf-8")
print unicode("[+] 完成 !","utf-8")
print unicode("[+] 共修改%s个目录和文件名"%fdcount[0],"utf-8")
elif sys.argv[2]=="-fp":
Modifypostfix(sys.argv[1],sys.argv[3],sys.argv[4])
print unicode("\n[+] Modifer.py    Build by LandGrey","utf-8")
print unicode("[+] 完成 !","utf-8")
print unicode("[+] 共修改%s个后缀名"%typecount[0],"utf-8")
elif sys.argv[2]=="-all":
Modifypostfix(sys.argv[1],sys.argv[3],sys.argv[4])
Modifyprefix(sys.argv[1],sys.argv[3],sys.argv[4])
print unicode("\n[+] Modifer.py    Build by LandGrey","utf-8")
print unicode("[+] 完成 !","utf-8")
print unicode("[+] 共修改%s个目录名、文件名和后缀名"%(typecount[0]+fdcount[0]),"utf-8")
else:
print unicode("\n[+] 选项错误 !\n","utf-8")
print unicode("[+] 用 -h 或--help 参数查看Modifer.py用法","utf-8")
sys.exit()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息