删除文件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