os.Open中没有此类文件或目录错误 [英] No such file or directory error from os.Open

查看:76
本文介绍了os.Open中没有此类文件或目录错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力找出os.Open的问题.在使用os.Open时,我一直收到没有这样的文件或目录"错误.因此,我的文件夹MVCApp中带有bin pkg src和模板.然后我在src文件夹中有main.所以我有这段代码:

I am struggling to figure out an issue with os.Open. I keep getting a 'no such file or directory' error when using os.Open on a folder named templates. So I have my folder MVCApp with bin pkg src and templates in it. Then I have main in the src folder. So I have this code:

basePath := "templates"
templateFolder, err := os.Open(basePath)
if err != nil {
    log.Fatal(err)
}
defer templateFolder.Close()

我认为这与我在开放环境中使用的basePath有关,但是我无法弄清楚我需要如何为它们找到该目录添加模板"的开头.我确信这是一个简单的解决方法,但是让我感到困惑.任何帮助将不胜感激.

I think it has something to do with the basePath that I am using with the open, but I cannot figure out how I need to preface "templates" in order for it to find this directory. I am sure this is an easy fix, but it has me perplexed. Any help would be greatly appreciated.

推荐答案

根据我们在评论中的讨论,事实证明,问题是由在错误的工作目录中运行的应用程序引起的.

Per our discussion in the comments, it turned out that the issue was caused by the application running in an incorrect working directory.

解决方法是修改路径,例如:

The fix was to modify the path, as such:

basePath := "../../templates"

您也可以使用os.Chdir永久更改工作目录(在整个过程中),并避免使用../...此处提供更多信息: https://golang.org/pkg/os/#File.Chdir

You can also use os.Chdir to change the working directory permanently (for the duration of the process), and avoid using the ../... More info here: https://golang.org/pkg/os/#File.Chdir

这篇关于os.Open中没有此类文件或目录错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆