zoukankan      html  css  js  c++  java
  • python开发 -- 批量修改文件后缀名为指定格式

    样例代码:

    #!/usr/bin/env python
    #coding:utf8
    #! python3
    #批量修改一个文件下的文件后缀
    import sys
    import os
    def Rename():
      #Path = "F:\test\"  # windows下的文件目录
      Path = input("请输入你需要操作的目录(格式如 --windows 'F:\test' ---mac '/Users/mac/Documents/test'):")
      # 也可以将文件路径写死:
      Path = '/Users/mac/Documents/test'
      
      filelist = os.listdir(Path)
      for files in filelist:
        Olddir = os.path.join(Path,files)
        print(files)  #打印出老的文件夹里的目录和文件
        if os.path.isdir(Olddir):  #判断是否是文件夹,是文件夹,跳过
          continue
        filename = os.path.splitext(files)[0]
        #filetype = os.path.splitext(files)[1]
        Newdir = os.path.join(Path,filename + '.xml')  #只要修改后缀名就可以更改成任意想要的格式
        os.rename(Olddir,Newdir)
    Rename()
    
    Newdir = os.path.join(Path,str(random.randint(1,1000))+filetype)
    # 修改文件名为随机名称
  • 相关阅读:
    Javascript调用C#后台方法及JSon解析
    ul 仿 table 循环滚动
    windows服务
    simple demo how to get the list of online users
    IIS 4.0配置
    在线
    C学习笔记-多源文件的编译
    C学习笔记-多源文件的编译
    C学习笔记-函数
    C学习笔记-函数
  • 原文地址:https://www.cnblogs.com/hellojesson/p/12859362.html
Copyright © 2011-2022 走看看