Here's a modified version of the Mohit script. Instead of deleting files with the wrong name, it non-destructively renames them.
It also replaces os.system () calls with subprocess calls, which solves problems with quotes in file names.
import glob import subprocess import os import re import logging import traceback filelist=glob.glob("/path/to/*.jpg") for file_obj in filelist: try: jpg_str = subprocess.check_output(['file', file_obj]).decode() if (re.search('PNG image data', jpg_str, re.IGNORECASE)) or (re.search('Png patch', jpg_str, re.IGNORECASE)): old_path = os.path.splitext(file_obj) if not os.path.isfile(old_path[0]+'.png'): new_file = old_path[0]+'.png' elif not os.path.isfile(file_obj+'.png'): new_file = file_obj+'.png' else: print("Found PNG hiding as JPEG but couldn't rename:", file_obj) continue print("Found PNG hiding as JPEG, renaming:", file_obj, '->', new_file) subprocess.run(['mv', file_obj, new_file]) except Exception as e: logging.error(traceback.format_exc()) print("Cleaning JPEGs done")
Different55 Apr 15 '18 at 23:19 2018-04-15 23:19
source share