我在循环使用此功能时遇到麻烦,导致出现错误,该错误表明该文件仍在按如下方式使用:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\Will\\Dropbox\\Python\\portfolio\\_temp\\15219045498121354237513144543229969014.wav'
我相信这是由于在我尝试将另一个文件重命名为与要删除的文件相同的名称时,该文件仍被删除。这个准确吗?以及如何解决此类问题?
def splice(filepath: str, start, duration, save_as: str):
"""
@param filepath: absolute filepath to wav file
@param start: startime in seconds/str/timestamp
@param duration: duration in seconds/str/timestamp
@param save_as: filepath for new file
"""
# initialize
if not save_as: save_as = filepath
filepath = os.path.abspath(filepath)
save_as = os.path.abspath(save_as)
if filepath[-4:] != '.wav': filepath += '.wav'
if save_as[-4:] != '.wav': save_as += '.wav'
if filepath == save_as:
save_as = os.path.join(os.path.dirname(filepath),
'_temp_'+str(random.getrandbits(128))+'.wav')
# perform operation
start = audio_tools.convert.timestamp_to_seconds(start)
duration = audio_tools.convert.timestamp_to_seconds(duration)
result = os.system("ffmpeg -y -ss " + str(start) + \
" -t " + str(duration) + \
" -i \"" + filepath + "\"" + \
" \"" + save_as + "\"")
assert result == 0, "Error in creating file"
# delete original and rename temp file
if '_temp_' in os.path.basename(save_as):
os.remove(filepath)
os.rename(save_as,filepath)
之所以如此,是因为我使用了保管箱目录作为工作目录。这引起了冲突。