问题描述
我在使用 Python 处理 Mac 时有时会收到此错误。看似无缘无故,有时当前工作目录会成为您在图像中看到的内容,而有时则是 pyde 文件所在文件夹的工作目录。
关于为什么会发生这种情况的任何想法?
解决方法
为了避免这样的问题,我总是尝试使用绝对路径。我建议你尝试这样的文件路径:
import os
# This will be the path to your .py file
FILE_PATH = os.path.dirname(os.path.abspath(__file__))
# This will be the path to your text file,if it is in the same directory as the .py
LEVELS_FILE_PATH = os.path.join(FILE_PATH,"levels.txt")
然后,您可以拥有以下内容,而不是您当前的公开声明:
f = open(LEVELS_FILE_PATH,'r')