【问题标题】:Why does the terminal search the working directory for files that an executing file is searching for?为什么终端在工作目录中搜索执行文件正在搜索的文件?
【发布时间】:2013-03-19 05:34:31
【问题描述】:

如果标题不够清晰,目录中有 Developer/Pythonfiles/Pong,其中有 Pong.py 和 hit.wav。 当我运行~ $python Developer/Pythonfiles/Pong/Pong.py
声音 hit.wav 不会播放,而是播放奇怪的爆裂声。 但是当我运行~/Developer/Pythonfiles/Pong $python Pong.py 时,hit.wav 的声音会按预期播放。

为什么程序 Pong.py 不搜索它所在的目录而不是我的工作目录来查找 Hit.wav?我尝试使用hit = pygame.mixer.Sound("hit.wav") 访问hit.wav 并使用hit.play() 播放它

【问题讨论】:

  • 贴出你的代码,你可以得到不靠猜测的答案。

标签: python python-2.7 terminal pygame


【解决方案1】:

因为它无法自动知道这就是您想要的。因此,请自己指定所需的行为:

import os
hit = pygame.mixer.Sound(os.path.join(os.path.dirname(__file__), "hit.wav"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-07
    • 1970-01-01
    • 2018-04-26
    • 2015-06-07
    • 1970-01-01
    • 2014-01-29
    • 1970-01-01
    • 2010-09-05
    相关资源
    最近更新 更多