import os
import shutil
from pytube import YouTube
import random
import string
import uuid
import time
from datetime import date
from datetime import datetime
from time import gmtime, strftime
from moviepy.editor import VideoFileClip, VideoClip, AudioFileClip, AudioClip
import tkinter as tk
from tkinter import filedialog
import random
yt = YouTube
print ('What youtube video would you like too download and convert to .mp3?(Insert Link)')
ytv = input()
try:
lol = yt(ytv).streams.first().download()
except:
print ('That is not a YouTube link')
exit()
today = strftime('%Y-%m-%d', gmtime())
video1 = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Videos')
title = yt(ytv).streams[0].default_filename
title1 = '\' + yt(ytv).streams[0].default_filename
random = uuid.uuid4().hex.upper()[0:6]
name = random + '-' + today + '.mp4'
current = os.getcwd()
os.rename(current + title1, name)
shutil.move(current + '\' + name, video1)
random = random = uuid.uuid4().hex.upper()[0:6]
video = VideoFileClip(video1 + '\' + name)
print ('The audio file will be in your downloads folder')
save = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Downloads')
video.audio.write_audiofile(save + '\' + random + '.mp3')
This whole script just downloads a youtube video, sends it to a folder, that runs an api that converts that downloaded video to an mp3 file. Right here I want to remove that file so the user just has a .mp3 but it has some sort of lock on it that I can't unlock/figure out.
os.remove(video1 + '\' + name)
question from:https://stackoverflow.com/questions/65621465/how-to-delete-a-file-getting-used-in-a-python-script