Exception has occurred: PermissionError [WinError 32] The process cannot access the file because it is being used by another process:

I'm getting the following error when I run my code (the error occurs at the *** line):

Exception has occurred: PermissionError
[WinError 32] The process cannot access the file because it is being used by another process: 'pexels-joshua-woroniecki-9073157.mp4' #this file is one of two files found in my stockFootage directory

I'm trying to delete a file, but it's telling me I got a permission error. Does anyone know how I could fix this? Any help would be greatly appreciated. Thank you.

My Code:

chdir(r'C:\Users\jack_l\Documents\REDDIT_TO_YOUTUBE_PYTHON_SELENIUM\redditVideo\stockFootage')
myStockFootage = next(walk(r'C:\Users\jack_l\Documents\REDDIT_TO_YOUTUBE_PYTHON_SELENIUM\redditVideo\stockFootage'), (None, None, []))[2]
stockFootage = VideoFileClip(myStockFootage[0], target_resolution=(1080, 1920))
stockFootage = stockFootage.without_audio()
stockFootage = stockFootage.loop(duration = mergedVideos.duration)
stockFootage.write_videofile('loopedStock.mp4', fps = 30)

for counter24 in range(len(myStockFootage)):
    if 'loopedStock' not in myStockFootage[counter24]:
***     os.remove(myStockFootage[counter24])

chdir(r'C:\Users\jack_l\Documents\REDDIT_TO_YOUTUBE_PYTHON_SELENIUM\redditVideo\finalVideo')
finalVideo = CompositeVideoClip([stockFootage, commentVideo])
finalVideo.write_videofile('finalVideo.mp4', fps=30)

print('Finished')

Upvotes: 0

Views: 10899

Answers (1)

Grismar
Grismar

Reputation: 31319

From the comments it is clear that you're either not sharing the code causing the actual problem, or making more changes than was suggested in the comments.

Take this:

from shutil import copy
from os import remove
import moviepy.video.io.VideoFileClip as VideoFileClip

copy('test.mp4', 'new_test.mp4')
stock_footage = VideoFileClip.VideoFileClip(r'new_test.mp4', target_resolution=(1080, 1920))
try:
    remove('new_test.mp4')
except PermissionError:
    print('as expected')
stock_footage .close()
try:
    remove('new_test.mp4')
    print('success')
except PermissionError:
    print('you will not see this')

Output (assuming you have a test.mp4 in the same location as the script):

as expected
success

Which shows that VideoFileClip locks the file it opens and calling .close() on it resolves the issue.

Upvotes: 1

Related Questions