0

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/

Emil
  • 1,531
  • 3
  • 22
  • 47
  • Does this answer your question? [Why am I getting a FileNotFoundError?](https://stackoverflow.com/questions/17658856/why-am-i-getting-a-filenotfounderror) – enzo Jul 02 '21 at 22:56
  • No, that's not it. I have set my working directory correctly and the name of the .mp3 file is correct as well. – Emil Jul 02 '21 at 22:59
  • How are you setting your working directory? Can you share your project structure? From where are you running your Python script? – enzo Jul 02 '21 at 23:00
  • What do you mean by project structure? I just used: `os.chdir(DIRECTORY)` – Emil Jul 02 '21 at 23:08
  • Check if `os.getcwd()` reports the expected directory. – DYZ Jul 02 '21 at 23:12
  • Yes, I tried and it does – Emil Jul 02 '21 at 23:19
  • `os.listdir()` also shows `'Interview-part2.mp3'` as one of the files in the directory – Emil Jul 02 '21 at 23:31
  • Apparently you do not *have set my working directory correctly*, because if you had, you wouldn't be having this issue. Specify the full path name of the file by hard-coding it. Does that fix the issue? If so, then you've not set your working directory properly. – Ken White Jul 03 '21 at 03:06
  • I have set it correctly. Otherwise, I wouldnt see the correct files when running `os.listdir()` – Emil Jul 03 '21 at 13:58

2 Answers2

0

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'
aboellinger
  • 156
  • 1
  • 6
0

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

john_orge
  • 11
  • 3