删除文件PermissionError: [WinError 32] 另一个程序正在使用此文件,进程无法访问。

删除文件PermissionError: [WinError 32] 另一个程序正在使用此文件,进程无法访问。

问题描述

是想写一个脚本删除长宽比不对的图片
在这里插入图片描述

# coding: utf-8
from PIL import Image, ImageDraw, ImageFont
import os
import shutil
import cv2 as cv
import numpy as np
import json

#删除侧面图象

def read_json(file_name):
    with open(file_name, 'rb') as f:
        data = json.load(f)
    return data

#将长宽比不对的删除


if __name__ == "__main__":
    #my_copy("./1/kuaisu.json","D:\\code\\select\\1\\","D:\\code\\select\\final\\")
    image_root_path = "D:\\code\\select\\delete1\\no"
    suffix = ".bmp"
    delImg = []
    for root, dirs, files in os.walk(image_root_path):
        # 使用os模块获取文件夹中所有文件的路径
        all_files = os.listdir(root)
        filtered_files = [file for file in all_files if file.endswith(suffix)]
        if (len(filtered_files)):
            # print(root)  #当前工作文件夹
            for i in range(len(filtered_files)):
                bmp_path = root + "\\" + filtered_files[i]
                # print(bmp_path)
                first_image = Image.open(bmp_path)
                width, height = first_image.size
                print(width)
                print(height)
                if (height / width > 3):
                    # os.remove(bmp_path)
                    delImg.append(bmp_path)
                    print(bmp_path)
                print("=======")

    for i in range(len(delImg)):
        os.remove(delImg[i])

在这里插入图片描述

解决方案

是因为读取文件没有关闭导致了,我这里找不到块号,不好使用OS.close就只能代码封装了

# coding: utf-8
from PIL import Image, ImageDraw, ImageFont
import os
import shutil
import cv2 as cv
import numpy as np
import json

#删除侧面图象

def read_json(file_name):
    with open(file_name, 'rb') as f:
        data = json.load(f)
    return data

#将长宽比不对的删除
def delete1(image_root_path,suffix):
    delImg = []
    for root, dirs, files in os.walk(image_root_path):
        # 使用os模块获取文件夹中所有文件的路径
        all_files = os.listdir(root)
        filtered_files = [file for file in all_files if file.endswith(suffix)]
        if (len(filtered_files)):
            # print(root)  #当前工作文件夹
            for i in range(len(filtered_files)):
                bmp_path = root + "\\" + filtered_files[i]
                # print(bmp_path)
                first_image = Image.open(bmp_path)
                width, height = first_image.size
                print(width)
                print(height)
                if (height / width > 3):
                    # os.remove(bmp_path)
                    delImg.append(bmp_path)
                    print(bmp_path)
                print("=======")
    return delImg

if __name__ == "__main__":
    #my_copy("./1/kuaisu.json","D:\\code\\select\\1\\","D:\\code\\select\\final\\")
    delImg = delete1("D:\\code\\select\\delete1\\noo",".bmp")

    for i in range(len(delImg)):
        os.remove(delImg[i])

在这里插入图片描述

另外一个问题

由于使用的是机械硬盘,读取速度很慢,每次删除了后,又还有剩余文件没删干净(估计是程序执行时间太短,但是硬盘读取时间长,文件没有读取到)

解决方案

# coding: utf-8
from PIL import Image, ImageDraw, ImageFont
import os
import shutil
import cv2 as cv
import numpy as np
import json
from time import sleep

#删除侧面图象

def read_json(file_name):
    with open(file_name, 'rb') as f:
        data = json.load(f)
    return data

#将长宽比不对的删除
def delete1(image_root_path,suffix):
    delImg = []
    for root, dirs, files in os.walk(image_root_path):
        # 使用os模块获取文件夹中所有文件的路径
        all_files = os.listdir(root)
        filtered_files = [file for file in all_files if file.endswith(suffix)]
        if (len(filtered_files)):
            # print(root)  #当前工作文件夹
            for i in range(len(filtered_files)):
                bmp_path = root + "\\" + filtered_files[i]
                # print(bmp_path)
                first_image = Image.open(bmp_path)
                width, height = first_image.size
                if (height / width > 3):
                    # os.remove(bmp_path)
                    delImg.append(bmp_path)
    return delImg

def mydel(delImg):
    for i in range(len(delImg)):
        os.remove(delImg[i])
        print(delImg[i])

def onestep(image_root_path,suffix,step):
    for i in range(step):
        delImg = delete1(image_root_path, suffix)
        mydel(delImg)
        sleep(1)



if __name__ == "__main__":
    #my_copy("./1/kuaisu.json","D:\\code\\select\\1\\","D:\\code\\select\\final\\")
    onestep("D:\\Datasets\\defect2\\final9.4\\yes", ".bmp",200)

在这里插入图片描述

参考1

参考2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值