You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Playsound模块无法找到同目录文件的问题求助

Fixing the "Cannot find specified file" Error with playsound and Sound.mp3

Hey there, let's troubleshoot this playsound issue together! Even though you placed Sound.mp3 in the same folder as your script, the error points to path or module-related problems. Here are actionable fixes to try:

  • Check your current working directory first
    Sometimes your script runs from a different folder than you think! Add this code at the top of your script to confirm:

    import os
    print("Current working directory:", os.getcwd())
    

    If the printed path isn't where your script and Sound.mp3 live, you have two easy fixes:

    1. Run the script directly from its folder (open a terminal/command prompt in that folder, then run python your_script.py)
    2. Use the script's directory to build a reliable path automatically:
      from pathlib import Path
      import playsound
      
      sound_path = Path(__file__).parent / "Sound.mp3"
      playsound.playsound(sound_path)
      
  • Verify filename exactness
    On Linux/macOS, filenames are case-sensitive—make sure your code uses Sound.mp3 (not sound.mp3 or SOUND.MP3). Even on Windows (which is case-insensitive), matching the exact filename avoids unexpected issues.

  • Update or downgrade playsound
    The playsound module has known path-related bugs in newer versions. Try rolling back to a stable release:

    pip uninstall playsound
    pip install playsound==1.2.2
    

    This older version resolves many file path and MCI device errors for most users.

  • Confirm the file is valid and in the right place
    Double-click Sound.mp3 to make sure it plays normally (corrupted files can trigger odd errors). Also, check that it's not hidden in a subfolder, and hasn't been accidentally renamed with a hidden extension like Sound.mp3.txt (enable file extensions in your file explorer to verify).

  • Test with an absolute path
    As a quick sanity check, use the full absolute path to your file in the playsound call. For example:

    # Windows example
    playsound.playsound("C:\\Users\\YourName\\Documents\\ScriptFolder\\Sound.mp3")
    # Linux/macOS example
    playsound.playsound("/home/yourname/Documents/ScriptFolder/Sound.mp3")
    

    If this works, the issue is definitely with relative path resolution.

内容的提问来源于stack exchange,提问作者EverythingDoesn'tMakeSense

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 20:33:12