win32权限:无法删除文件 [英] win32 permission: Cannot delete file

查看:115
本文介绍了win32权限:无法删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是之前提出的问题.我已经尝试了这些关于stackoverflow的查询中提供的所有可能的解决方案,但是都没有解决.

I know that this is a question that's been asked before. I have tried all the possible solutions provided in those queries on stackoverflow but none of them has worked out.

所以我有这个python代码,我试图在其中专门删除文件,音频文件(WAV格式).我不知道为什么Windows不允许我删除文件.

So i have this python code in which I am trying to delete files,audio files(of wav format) specifically. I don't know why windows won't let me delete files.

这里发生错误----> os.remove(j)PermissionError:[WinError 32]该进程无法访问文件,因为该文件正在被另一个进程使用:'æ1.wav'

The error occurs here----> os.remove(j) PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'æ1.wav'

这已经刺了我一段时间了.因为除了当前使用的文件以外,我没有看到该文件被打开或使用过.

This has been pricking me for a while now. Because I don't see this file being open or used anywhere else apart from the one in which I am using currently.

import soundfile as sf
import os

phon=['a','æ', 'ʌ','ɔ','ɑʊ', 'ɑɪ', 'b', 'ʧ', 'd', 'ð', 'ɛ', 'ɜɹ', 'eɪ', 'f', 'ɡ', 'h', 'i', 'ɪː', 'ʤ', 'k','l','m','n', 'ŋ', 'oʊ',
       'p','ɹ', 's', 'ʃ', 't', 'θ', 'ʊ', 'u', 'v', 'w', 'j', 'z','ʒ','ɔɪ' ]

count=0
for i in phon:
    path='C:\\Users\\enviz\\pron_dictionaries-master\\all_words\\all_phonemes\\'+i
    os.makedirs(path , exist_ok=True)
    os.chdir(path)
    x=len(os.listdir(os.getcwd())) 
    files = os.listdir(path)


    for j in files:
        f = sf.SoundFile(j)
        duration = (len(f) / f.samplerate)
        size_of_audio = (os.path.getsize(j))/1024


        #get rid of files less than 3KB
        if(size_of_audio<=3 or duration<0.15):

                os.remove(j) #the error is on this line

推荐答案

我认为您已打开文件.您需要按照文档中 中所述将其关闭:

I think you have the file open. You need to close it as stated in the docs:

如果打开了一个文件,则只要SoundFile对象存在,它就会一直保持打开状态.当对象被垃圾回收时,文件关闭,但是您应该使用soundfile.SoundFile.close()方法或上下文管理器显式关闭文件

If a file is opened, it is kept open for as long as the SoundFile object exists. The file closes when the object is garbage collected, but you should use the soundfile.SoundFile.close() method or the context manager to close the file explicitly

这篇关于win32权限:无法删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆