擅长:python、mysql、java
<p>我对你的理解正确吗?你只需要从文件中读取内容?如果您想阅读完整的内容,请使用如下内容(Python>;=2.5):</p>
<pre><code>from __future__ import with_statement
with open(my_file_name, 'r') as fp:
content = fp.read()
</code></pre>
<p><strong>注意:</strong>from\uu future_uu行必须是.py文件中的第一行(或可以放在第一行的内容编码规范之后)</p>
<p>或者旧方法:</p>
^{pr2}$
<p>如果您的文件包含非ascii字符,您还应该查看“编解码器”页:-)</p>
<p>然后,根据您的示例,最后一节可以如下所示:</p>
<pre><code>from __future__ import with_statement
@route(':filename')
def static_file(filename):
mylookup = TemplateLookup(directories=['html'])
mytemplate = mylookup.get_template('hello/index.html')
content = ''
with open('name.html', 'r') as fp:
content = fp.read()
return mytemplate.render(name=content)
</code></pre>
<p>您可以在官方文档中找到有关<a href="http://docs.python.org/library/stdtypes.html#file-objects" rel="nofollow noreferrer">file object</a>的更多详细信息:-)</p>
<p>还有一个快捷版本:</p>
<pre><code>content = open('name.html').read()
</code></pre>
<p>但我个人更喜欢有明确结尾的长版本:-)</p>