[英]I've got an error when trying to create sound using pygame
嘗試使用 pygame 添加聲音時出現錯誤。 我已將所有 .wav 文件與 python 腳本放在同一目錄中。 這是我第一次使用pygame,所以我一無所知。
import pygame
pygame.init()
pygame.mixer.init()
try:
person_sound = pygame.mixer.Sound("person.wav")
pygame.mixer.Sound.play(person_sound)
except:
import traceback
traceback.print_exc()
這是我所擁有的:
pygame 1.9.5 Hello from the pygame community. https://www.pygame.org/contribute.html Traceback (most recent call last): File "G:\\Desktop\\Sound Test\\Sound-Test.py", line 7, in <module> person_sound = pygame.mixer.Sound("person.wav") FileNotFoundError: No such file or directory. [Finished in 1.345s]
該文件不必與 python 文件在同一目錄中,但必須在應用程序的工作目錄中。
可以通過以下方式調查差異:
import os
currentWorkDir = os.getcwd()
print(currentWorkDir)
sourceFileDir = os.path.dirname(os.path.abspath(__file__))
print(sourceFileDir)
另請參閱與導入相關的模塊屬性。
應用程序可以更改當前工作目錄,使其與 python 源文件目錄相同:
import os
sourceFileDir = os.path.dirname(os.path.abspath(__file__))
os.chdir(sourceFileDir)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.