之所以有这个程序是因为做了个爬虫,URL中并没有文件名,默认保存可能无扩展名,然后需要进行修正。本程序遍历工作文件夹和子文件夹的所有文件。
#!/usr/bin/env python
# -*- coding:utf-8 -*-
###
# File: fix_png_suffix.py
# Created Date: 2023-03-21 10:50:08
# Author: Simon Liu
# -----
# Last Modified: 2023-03-23 20:08:13
# Modified By: Simon Liu
# -----
# Copyright (c) 2023 SimonLiu Inc.
#
# May the force be with you.
# -----
# HISTORY:
# Date By Comments
# ---------- --- ----------------------------------------------------------
###
import os,time
import imghdr
# 获取当前工作目录
root_folder = os.getcwd()
def fix_suffix(entry):
root, ext = os.path.splitext(entry)
type = imghdr.what(entry)
ext_no_dot = ext.lstrip(".")
if type: # 如果文件是图片
print(f"{entry}是个图像文件,扩展名{ext},实际类型{type}")
if ext_no_dot != type:
filename = '.'.join([root,type])
print(f"⚠️ 修正{entry}后缀名,新文件{filename}")
os.rename(entry, filename)
else:
print("文件无需修正")
def recursive_check(root_folder):
for entry in os.scandir(root_folder):
if entry.is_dir():
recursive_check(entry.path)
elif entry.is_file():
fix_pic_suffix(entry)
time.sleep(1)
recursive_check(root_folder)