Commit d93added authored by 吕祺's avatar 吕祺 ⛹🏽

pic compress

parent 08d7c3b3
import os
import re
import sys
dir = r"C:\Users\lq\Downloads\newpic"
def renameall():
fileList = os.listdir(dir) #待修改文件夹
print("修改前:"+str(fileList)) #输出文件夹中包含的文件
currentpath = os.getcwd() #得到进程当前工作目录
os.chdir(dir) #将当前工作目录修改为待修改文件夹的位置
num=1 #名称变量
for fileName in fileList: #遍历文件夹中所有文件
pat="(.+)\.(jpg|png|gif|jpeg)" #匹配文件名正则表达式
pattern = re.findall(pat,fileName) #进行匹配
# print(str(num)+'.'+pattern[0])
print(pattern[0][0].replace("-min", "") + "." + pattern[0][1])
os.rename(fileName, pattern[0][0].replace("-min", "") + "." + pattern[0][1])
# os.rename(fileName,(str(num)+'.'+pattern[0])) #文件重新命名
# num = num+1 #改变编号,继续下一项
# print("---------------------------------------------------")
# os.chdir(currentpath) #改回程序运行前的工作目录
# sys.stdin.flush() #刷新
# print("修改后:"+str(os.listdir(dir))) #输出修改后文件夹中包含的文件
renameall()
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment