import os
import imghdr
import shutil
import stat
import tensorflow as tf
def judge_wrong_file(root_path, file_path):
if imghdr.what(file_path) is None:
move(root_path, file_path)
else:
try:
img = tf.io.read_file(file_path)
tf.io.decode_jpeg(img, channels=0)
except:
move(root_path, file_path)
def move(root_path, wrong_file_path):
print(wrong_file_path)
# folder_path = os.path.dirname(root_path) + "_del"
root_path = root_path.split("/")
folder_path = os.path.join(root_path[1] + "_del", root_path[-1])
if not os.path.exists(folder_path):
os.makedirs(folder_path)
shutil.move(wrong_file_path, folder_path)
def move_reset(root_path):
folder_path = r"./dog_test_del/"
for root_del, dirs_del, files_del in os.walk(folder_path):
for file_del in files_del:
a_file = os.path.join(root_del, f
损坏文件移除脚本
本文介绍了一种使用脚本自动化删除系统中损坏无法正常打开的文件的方法,适用于维护系统整洁和优化存储空间。详细阐述了脚本的编写思路和实现过程,包括检查文件完整性的策略以及如何安全地移除有问题的文件。
摘要由CSDN通过智能技术生成