使用cv2时关闭Python文件

时间:2018-08-26 18:51:36

标签: python cv2

提前道歉以解决问题,我是一名大学生,正在学习c ++,并且第一次在个人项目中使用python。

我正在编写一个程序,该程序从目录或子目录中的媒体文件中提取标题,然后查看是否有匹配的字符串。如果存在,它将比较其分辨率,并删除较低分辨率的文件。如果它们的分辨率相同,则会删除较大的文件。除删除文件外,所有其他功能均有效。当我尝试这样做时,会引发错误消息,表明文件正在使用中。经过研究后,我得知这是因为我在代码内打开了文件,从而防止了文件被删除。我的问题是我不知道需要关闭哪个变量,也不知道要关闭的合适方法和位置。

import os
import cv2
import PTN
import json

array1 = [os.path.join(r,file) for r,d,f in os.walk("E:\Python Test Environment") for file in f]

for x in range(0, len(array1)):
    print(array1[x])

array2 = array1[:] #The colon tells it to directly copy rather than do a link

for x in range(0, len(array2)):
    array2[x] = (json.dumps(PTN.parse(array2[x])))
    array2[x] = json.loads(array2[x])['title']
    head, array2[x] = os.path.split(array2[x])
    del head

y = len(array2)

for x in range(0, len(array2)):
    if array2[x] == "":
        break

    for i in range(x, y-1): #Set to x+1 so that it does not compare against the current file
        i = x + 1

        if array2[i] == "":
            break

        if array2[x] == array2[i]:
            print 'Match found!'
            print array1[i]
            print 'Matches: '
            print array1[x]

            with open(array1[x]) as f: #tried to include this to prevent error, doesn't seem to stop it
                capture1 = cv2.VideoCapture(array1[x]) #Open the video
                ret, frame = capture1.read() #Read the first frame
                resolution1 = frame.shape #Get resolution
                f.close()

            with open(array1[i]) as f: #tried to include this to prevent error, doesn't seem to stop it
                capture2 = cv2.VideoCapture(array1[i]) #Open the video
                ret, frame = capture2.read() #Read the first frame
                resolution2 = frame.shape #Get resolution
                f.close()

            if resolution1 > resolution2:
                print array1[x]
                print "Is higher resolution than"
                print array1[i]
                print "Would delete: "
                print array1[i]
                os.remove(array1[i])
                array1[i] = ""
                array2[i] = ""

            if resolution2 > resolution1:
                print array1[i]
                print "Is higher resolution than"
                print array1[x]
                print "Would delete: "
                print array1[i]
                os.remove(array1[x])
                array1[x] = ""
                array2[x] = ""

            if resolution1 == resolution2:
                print "equal"
                if os.path.getsize(array1[x]) <= os.path.getsize(array1[i]):
                    print "Would delete: "
                    print array1[i]
                    os.remove(array1[i])
                    array1[i] = ""
                    array2[i] = ""

                if os.path.getsize(array1[i]) < os.path.getsize(array1[x]):
                print "Would delete: "
                print array1[x]
                os.remove(array1[x])
                array1[x] = ""
                array2[x] = ""

1 个答案:

答案 0 :(得分:1)

添加capture1.release()capture2.release()以释放VideoCapture实例使用的资源