python实现遍历文件夹修改文件后缀

yipeiwu_com6年前Python基础

本文实例为大家分享了python实现遍历文件夹修改文件后缀的具体代码,供大家参考,具体内容如下

用法 python Modifer.py ./ -fp java xml

# 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()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python从入门到精通(DAY 3)

python从入门到精通(DAY 3)

要求:编写登陆接口 输入用户名密码 认证成功后显示欢迎信息 输错三次后锁定 针对此实例写了有二种类型的脚本,略有不同,具体如下: 帐号文件account.txt内容如下: sam 12...

python数据预处理之将类别数据转换为数值的方法

在进行python数据分析的时候,首先要进行数据预处理。 有时候不得不处理一些非数值类别的数据,嗯, 今天要说的就是面对这些数据该如何处理。 目前了解到的大概有三种方法: 1,通过Lab...

Python微信操控itchat的方法

Python微信操控itchat的方法

itchat是一个开源的微信个人号接口,使用python调用微信从未如此简单。 开源地址 https://github.com/littlecodersh/ItChat 文档: http...

Python 描述符(Descriptor)入门

很久都没写 Flask 代码相关了,想想也真是惭愧,然并卵,这次还是不写 Flask 相关,不服你来打我啊(就这么贱,有本事咬我啊 这次我来写一下 Python 一个很重要的东西,即 D...

儿童python练习实例

实例一: 题目:有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少? 程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去 掉不满...