Python无法阅读目录

发布于 2025-02-08 08:09:56 字数 378 浏览 1 评论 0原文

因此,我有一个带有着色器代码的文件夹,在我的主要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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耀眼的星火 2025-02-15 08:09:56

将文件放在同一目录或子目录中是不够的。您还需要设置工作目录。
着色器文件路径必须相对于当前工作目录。工作目录可能与Python脚本的目录不同。
可以使用 __文件__ 。因此,Python脚本的绝对路径是os.path.abspath(__文件__))。当前的工作目录可以使用 os.chdir(path)

在代码开头复制以下内容,以将工作目录设置为与脚本目录相同的目录:

import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))

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 is os.path.abspath(__file__)). The current working directory can be changed with os.chdir(path).

Copy the following at the beginning of your code to set the working directory to the same as the script's directory:

import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文