Use python's built-in imghdr module to read image files to fix wrong extensions

The reason why this program exists is because a crawler is made, there is no file name in the URL, and the default save may have no extension, and then it needs to be corrected. This program iterates through all files in the working folder and subfolders.

#!/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)

おすすめ

転載: blog.csdn.net/toopoo/article/details/129738299