损坏文件移除脚本

本文介绍了一种使用脚本自动化删除系统中损坏无法正常打开的文件的方法,适用于维护系统整洁和优化存储空间。详细阐述了脚本的编写思路和实现过程,包括检查文件完整性的策略以及如何安全地移除有问题的文件。
摘要由CSDN通过智能技术生成
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_38185649

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值