I know that the question has often been asked but I can not solve the problem in my case:
I created a script that looks at a folder where I stocked audio file (.wav). All audio file that have more than 10 seconds duration a moved into another folder, the other are 0 padded to have only 10 secondes audio files:
from pydub import AudioSegment
import os
import shutil
path_to_sound = r'my\path\to\sound'
path_to_export = r'my\path\to\export'
path_for_too_long = r'my\path\to\other_directory'
pad_ms= 10000
file_names=os.listdir(path_to_sound)
print(file_names) # ['30368.wav', '41348.wav', '42900.wav', '42901.wav', '42902.wav']
for name in file_names:
audio= AudioSegment.from_wav(os.path.join(path_to_sound, name))
if pad_ms > len(audio):
shutil.move(os.path.join(path_to_sound, name), path_for_too_long)
else:
silence = AudioSegment.silent(duration=pad_ms-len(audio)+1)
padded = audio + silence
padded.export(os.path.join(path_to_export, name), format = 'wav')
I got the following error:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\path_to_sound"\\filename.wav'
Only Python with Jupyter Notebook is running. I guess that I need to close the file before I copy it. The problem is that i'm using file as str object, then i cant use close()