Python Playsound模块无法找到同目录文件的问题求助
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.mp3live, you have two easy fixes:- Run the script directly from its folder (open a terminal/command prompt in that folder, then run
python your_script.py) - 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)
- Run the script directly from its folder (open a terminal/command prompt in that folder, then run
Verify filename exactness
On Linux/macOS, filenames are case-sensitive—make sure your code usesSound.mp3(notsound.mp3orSOUND.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.2This older version resolves many file path and MCI device errors for most users.
Confirm the file is valid and in the right place
Double-clickSound.mp3to 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 likeSound.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

