FileNotFoundError: [Errno 2] No such file or directory: 'Interview-part2.mp3' - pydub

1.5k Views Asked by At

I am trying to convert a .mp3 file into a .wav file based on [this website][1].

I have set my working directory to the location that stores both the python script and the .mp3 file, and tried running the code below:

from os import path
from pydub import AudioSegment

#files
src = 'Interview-part2.mp3'
dst = 'Interview-part2.wav'
    
#Convert mp3 to wav
sound = AudioSegment.from_mp3(src)

However, when I run this, I get the following error:

Traceback (most recent call last):

  File "<ipython-input-19-f647e282d13d>", line 1, in <module>
    sound = AudioSegment.from_mp3(src)

  File "C:\Users\20200016\Anaconda3\lib\site-packages\pydub\audio_segment.py", line 796, in from_mp3
    return cls.from_file(file, 'mp3', parameters=parameters)

  File "C:\Users\20200016\Anaconda3\lib\site-packages\pydub\audio_segment.py", line 651, in from_file
    file, close_file = _fd_or_path_or_tempfile(file, 'rb', tempfile=False)

  File "C:\Users\20200016\Anaconda3\lib\site-packages\pydub\utils.py", line 60, in _fd_or_path_or_tempfile
    fd = open(fd, mode=mode)

FileNotFoundError: [Errno 2] No such file or directory: 'Interview-part2.mp3'

Instead of only providing the audio file, I have also tried:

src = DIRECTORY+'Interview-part2.mp3'

But this resulted in a FileNotFoundError as well.

What causes this error, and what can I do to overcome it? [1]: https://pythonbasics.org/convert-mp3-to-wav/

2

There are 2 best solutions below

0
On

use "." for absolute path in windows.
example: I have audio ext .mp3 in folder audio ./audio/1-18 TOEFL Exercise 4.mp3.

0
On

A good start for debugging this would be to expand src to an absolute path, and make sure it resolves to what you expect.

>>> src = 'Interview-part2.mp3'
>>> import os
>>> os.path.abspath(src)  # Result might strongly vary
'C:\\WINDOWS\\system32\\Interview-part2.mp3'