Golang的template.ParseFiles函数报错”not a directory”。

huangapple go评论147阅读模式
英文:

Golang template.ParseFiles "not a directory" error

问题

我正在尝试渲染一个模板:

root_path, err := osext.Executable()
if err != nil {
    return err
}
template_path := root_path + "/app/views/mailtemplates/" + "feedback.html"

fmt.Println(exist(template_path))

tmpl, err := template.ParseFiles(template_path)
if err != nil {
    return err
}

但是我遇到了"not a directory"的错误。
我的exist函数:

func exist(file_path string) bool {
    if _, err := os.Stat(file_path); os.IsNotExist(err) {
        return false
    }

    return true
}

它返回truetemplate.ParseFiles有什么问题?我查看了文档,上面写着参数应该是文件名,并且至少要有一个文件。我做错了什么?

编辑:
我的变量:

root_path: /home/cnaize/Dropbox/develop/gocode/bin/advorts
template_path: /home/cnaize/Dropbox/develop/gocode/bin/advorts/app/views/mailtemplates/feedback.html

我的文件内容:

<html>
    <head>
    </head>
    <body>
        Hello, World!
    </body>
</html>

**编辑2:**我把所有的项目、库等都移动到了/home/cnaize/gocode,现在我遇到了错误:

open /home/cnaize/gocode/bin/advorts/app/views/mailtemplates/feedback.txt: not a directory

这是Revel的问题吗?我应该怎么做?我尝试删除了这个bin文件,但没有帮助。

英文:

I'm trying to render just one template:

root_path, err := osext.Executable()
if err != nil {
	return err
}
template_path := root_path + &quot;/app/views/mailtemplates/&quot; + &quot;feedback.html&quot;

fmt.Println(exist(template_path))

tmpl, err := template.ParseFiles(template_path)
if err != nil {
	return err
}

but I have the error not a directory.
My exist function:

func exist(file_path string) bool {
    if _, err := os.Stat(file_path); os.IsNotExist(err) {
        return false
    }

    return true
}

it returns true. What's the problem with template.ParseFiles? I see documentation and where written that arguments are filenames and There must be at least one file. What I doing wrong?

EDITED:
My variables:

root_path: /home/cnaize/Dropbox/develop/gocode/bin/advorts
template_path: /home/cnaize/Dropbox/develop/gocode/bin/advorts/app/views/mailtemplates/feedback.html

My file's content:

&lt;html&gt;
	&lt;head&gt;
	&lt;/head&gt;
	&lt;body&gt;
		Hello, World!
	&lt;/body&gt;
&lt;/html&gt;

EDITED 2: I've moved all my projects, libs and etc to /home/cnaize/gocode and now I have error:

open /home/cnaize/gocode/bin/advorts/app/views/mailtemplates/feedback.txt: not a directory

Is it Revel's problem? What I supposed to do? I've tried to remove this bin file, didn't helped.

答案1

得分: 2

实际问题是Dropbox/文件夹内的路径:考虑到它是由外部进程(dropbox)同步/管理的,访问该文件的能力并不总是可靠的。

将文件放在Dropbox路径之外可以解决这个问题。

英文:

The actual issue was a path within the Dropbox/ folder: consider it is synchronized/managed by an external process (dropbox), the ability to access the file isn't always reliable.

Putting the file outside of that Dropbox path would solve the issue.

huangapple
  • 本文由 发表于 2014年7月15日 14:05:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/24750896.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定