liutianrui1

 

 

import os
import re
import sys
def renameall():
    #待修改文件夹
    fileList = os.listdir(r"C:\Users\xunbao\Desktop\Screenshots")
    #输出文件夹中包含的文件
    print("修改前:"+str(fileList))
    #得到进程当前工作目录
    currentpath = os.getcwd()
    #将当前工作目录修改为待修改文件夹的位置
    os.chdir(r"C:\Users\xunbao\Desktop\Screenshots")
    num=1       #名称变量
    for fileName in fileList:       #遍历文件夹中所有文件
        pat=".+\.(jpg|png|pgm)"     #匹配文件名正则表达式
        pattern = re.findall(pat,fileName)      #进行匹配
        os.rename(fileName,(str(num)+\'.\'+pattern[0]))       #文件重新命名
        num = num+1     #改变编号,继续下一项
    print("---------------------------------------------------")
    os.chdir(currentpath)       #改回程序运行前的工作目录
    sys.stdin.flush()       #刷新
    print("修改后:"+str(os.listdir(r"C:\Users\xunbao\Desktop\Screenshots")))       #输出修改后文件夹中包含的文件

renameall()

  

分类:

技术点:

相关文章:

  • 2021-12-23
  • 2021-12-23
  • 2021-10-19
  • 2021-10-19
  • 2021-10-19
  • 2021-11-29
  • 2021-12-30
猜你喜欢
  • 2021-11-23
  • 2021-10-30
  • 2021-10-18
相关资源
相似解决方案