[WinError 2]file not found error what is the problem?

from pydub import AudioSegment
import os

file_path = "your_music.mp3"
absolute_path = os.path.abspath(file_path)

audio = AudioSegment.from_file("your_music.mp3")

this code make a file not found error

  1. I installed os, pydub
  2. There is a file your_music.mp3
  3. I tried absolute_path.
  4. If I run “ls” code in python, the result show “your_music.mp3”

what is the problem???

  1. What folder is that file in?
  2. Why do you expect that Python will look in that folder for the file, and not any other folder?

Once you have considered these questions properly, if it was not a simple oversight, see here to understand how it actually works:

Exactly what code did you write to try this, and what happened when you tried it?

This makes no sense. ls isn’t Python code; it’s something you type at your computer’s command prompt.

Does absolute_path contain the correct path?

Hi thankyou for answer.

  1. C:\dev\music_proj is the path where file exist.
    2-1. I run git cmd, and move to "C:\dev\music_proj " this folder and run jupyter lab
    2-2. I make ipynb file at here.
    2-3. Because of the caused case, I run this code to check the directory where this python is.
    this is the code 2-3
import os

current_directory = os.getcwd()
print("directory:", current_directory)

2-4. To run absolute path I run the code below

from pydub import AudioSegment
import os

# run absolute path
file_path = "your_music.mp3"
absolute_path = os.path.abspath(file_path)
print(absolute_path)
audio = AudioSegment.from_file("your_music.mp3")

Thank you. I will read the link which you linked.

Yes I think it’s correct.

this is my code to run absolute path

from pydub import AudioSegment
import os

# run absolute path
file_path = "your_music.mp3"
absolute_path = os.path.abspath(file_path)
print(absolute_path)
audio = AudioSegment.from_file("your_music.mp3")

Can you copy paste the full error test including stack trace?

In my experience, a mysterious file not found error is often not about the file you think it is about. A fairly common source of errors is that something else is not found. In this case, pydub.AudioSegment uses ffmpeg.exe internally, so I’m slightly suspicious that maybe that executable is not found.

2 Likes

Okay, and what result do you get from the print call? Is it like you expect?

This won’t help, because os.path.abspath uses the same logic to decide how to create the absolute path.

  1. [code 1]
import os

# check directory
current_directory = os.getcwd()
print("directory:", current_directory)

[result]
directory: C:\dev\music_proj

  1. [code2] (there are some Korean languages)
ls

[result]
C 드라이브의 볼륨에는 이름이 없습니다.
볼륨 일련 번호: E6EE-B8FA

C:\dev\music_proj 디렉터리

2024-01-24 오후 01:28 .
2024-01-24 오전 10:15 …
2024-01-24 오전 10:16 .ipynb_checkpoints
2024-01-24 오후 01:28 12,625 music_project.ipynb
2023-11-01 오전 11:00 5,774,444 your_music.mp3
2개 파일 5,787,069 바이트
3개 디렉터리 1,840,239,951,872 바이트 남음

  1. [code3]
from pydub import AudioSegment
import os

# run absolute path
file_path = "your_music.mp3"
absolute_path = os.path.abspath(file_path)
print(absolute_path)
audio = AudioSegment.from_file("your_music.mp3")

[result]
C:\dev\music_proj\your_music.mp3

FileNotFoundError Traceback (most recent call last)
Cell In[13], line 8
6 absolute_path = os.path.abspath(file_path)
7 print(absolute_path)
----> 8 audio = AudioSegment.from_file(“your_music.mp3”)

File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\pydub\audio_segment.py:728, in AudioSegment.from_file(cls, file, format, codec, parameters, start_second, duration, **kwargs)
726 info = None
727 else:
→ 728 info = mediainfo_json(orig_file, read_ahead_limit=read_ahead_limit)
729 if info:
730 audio_streams = [x for x in info[‘streams’]
731 if x[‘codec_type’] == ‘audio’]

File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\pydub\utils.py:274, in mediainfo_json(filepath, read_ahead_limit)
271 file.close()
273 command = [prober, ‘-of’, ‘json’] + command_args
→ 274 res = Popen(command, stdin=stdin_parameter, stdout=PIPE, stderr=PIPE)
275 output, stderr = res.communicate(input=stdin_data)
276 output = output.decode(“utf-8”, ‘ignore’)

File ~\AppData\Local\Programs\Python\Python312\Lib\subprocess.py:1026, in Popen.init(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, user, group, extra_groups, encoding, errors, text, umask, pipesize, process_group)
1022 if self.text_mode:
1023 self.stderr = io.TextIOWrapper(self.stderr,
1024 encoding=encoding, errors=errors)
→ 1026 self._execute_child(args, executable, preexec_fn, close_fds,
1027 pass_fds, cwd, env,
1028 startupinfo, creationflags, shell,
1029 p2cread, p2cwrite,
1030 c2pread, c2pwrite,
1031 errread, errwrite,
1032 restore_signals,
1033 gid, gids, uid, umask,
1034 start_new_session, process_group)
1035 except:
1036 # Cleanup if the child failed starting.
1037 for f in filter(None, (self.stdin, self.stdout, self.stderr)):

File ~\AppData\Local\Programs\Python\Python312\Lib\subprocess.py:1538, in Popen._execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, unused_restore_signals, unused_gid, unused_gids, unused_uid, unused_umask, unused_start_new_session, unused_process_group)
1536 # Start the process
1537 try:
→ 1538 hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
1539 # no special security
1540 None, None,
1541 int(not close_fds),
1542 creationflags,
1543 env,
1544 cwd,
1545 startupinfo)
1546 finally:
1547 # Child is launched. Close the parent’s copy of those pipe
1548 # handles that only the child should have open. You need
(…)
1551 # pipe will not close when the child process exits and the
1552 # ReadFile will hang.
1553 self._close_pipe_fds(p2cread, p2cwrite,
1554 c2pread, c2pwrite,
1555 errread, errwrite)

FileNotFoundError: [WinError 2] 지정된 파일을 찾을 수 없습니다

Final error means = [WinError 2] Can’t find the file.

Okay, then @petersuter is right. If you didn’t already, you need to install the FFMpeg program (not a Python library, and nothing to do with Python) on your computer first. PyDub will work by launching that program as another process. If you already have FFMpeg installed, check the PyDub documentation to see how to make sure that it can find FFMpeg.

ffmpeg can be downloaded from here: Download FFmpeg