使用 cv2 时关闭 Python 个文件
Closing Python files while using 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] = ""
添加 capture1.release()
和 capture2.release()
以释放 VideoCapture
个实例使用的资源
提前为可能很简单的修复表示歉意,我是一名学习 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] = ""
添加 capture1.release()
和 capture2.release()
以释放 VideoCapture
个实例使用的资源