我将视频mp4文件转换为音频mp3格式,现在我想使用os.remove删除原始mp4视频文件,但给出错误

2024-04-25 20:18:40 发布

您现在位置:Python中文网/ 问答频道 /正文

之前,我将mp4视频文件转换为mp3音频文件。现在我想删除原始mp4视频文件使用

os.remove

但是,当我执行代码时,它会显示一个错误,如下所示:

Win32 error : The process cannot access the file because it is being used by another process

下面是我的代码:

try:
    global stram
    b2.config(text="Please wait...")
    b2.config(state=DISABLED)
    stream = yt.streams.filter(progressive=True)
    path = filedialog.askdirectory()
    if path == None:
        return
    stream[0].download(path)
    for i in os.listdir(path):
        os.rename(os.path.join(path,i),os.path.join(path,i.replace(' ','_')))
    title = yt.title.replace(' ','_')
    video = VideoFileClip(os.path.join(path+"/"+title+".mp4"))
    video.audio.write_audiofile(os.path.join(path+"/"+title+".mp3"))

    l3 = Label(action,text="Download Complete",font=("Calibri",12),fg = "green").pack()
    b2.config(text="Download Audio")
    try:
        file = str(f'{title}.mp4')
        os.remove(os.path.join(path,file))
    except Exception as e:
        print(e)
except Exception as e:
    l3 = Label(action,text="Error occured while Downloading",font=("Calibri",12),fg = "red").pack()

有人知道为什么会发生这种错误吗?非常感谢你的帮助。提前谢谢


1条回答
网友
1楼 · 发布于 2024-04-25 20:18:40

谈到windows,我不是专家,但我认为“另一个过程”实际上指的是您的python脚本,或者您在python脚本中使用的模块。我猜想您应该找到一种方法来关闭video变量

基于this,您应该能够在os.remove之前执行video.close()

相关问题 更多 >