Python无法阅读目录
因此,我有一个带有着色器代码的文件夹,在我的主要Python脚本(在着色器文件夹之外)中,我有一系列代码:
self.shader = self.createShader("shaders/vertex.txt", "shaders/fragment.txt")
但是它只是吐出此错误
filenotfounderror:[errno 2]没有这样的文件或目录:'Shaders/dertex.txt'
So, i have a folder with my shader code, in my main python script (outside of the shader folder) i have a line of code :
self.shader = self.createShader("shaders/vertex.txt", "shaders/fragment.txt")
but it just spits out this error
FileNotFoundError: [Errno 2] No such file or directory: 'shaders/vertex.txt'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将文件放在同一目录或子目录中是不够的。您还需要设置工作目录。
着色器文件路径必须相对于当前工作目录。工作目录可能与Python脚本的目录不同。
可以使用
__文件__
。因此,Python脚本的绝对路径是os.path.abspath(__文件__))
。当前的工作目录可以使用os.chdir(path)
。在代码开头复制以下内容,以将工作目录设置为与脚本目录相同的目录:
It is not enough to put the files in the same directory or sub directory. You also need to set the working directory.
The shader file path has to be relative to the current working directory. The working directory is possibly different to the directory of the python script.
The name and relative path of the python file can be retrieved with
__file__
. So the absolute path to the python script isos.path.abspath(__file__))
. The current working directory can be changed withos.chdir(path)
.Copy the following at the beginning of your code to set the working directory to the same as the script's directory: