Python 使用Flask显示特定目录中文件的内容

Python 使用Flask显示特定目录中文件的内容,python,flask,directory-structure,static-files,Python,Flask,Directory Structure,Static Files,我正在使用Flask实现一个应用程序,并试图显示我放在日志目录中的文本文件的内容。所以我做了这件事: @app.route('/files', methods = ['GET']) def config(): if 'username' in session : path = os.path.expanduser(u'~/path/to/log/') return render_template('files.html', tree=make_tree(p

我正在使用Flask实现一个应用程序,并试图显示我放在日志目录中的文本文件的内容。所以我做了这件事:

@app.route('/files', methods = ['GET'])
def config():
    if 'username' in session :
        path = os.path.expanduser(u'~/path/to/log/')
        return render_template('files.html', tree=make_tree(path))
    else:
        return redirect(url_for('login'))

def make_tree(path):
    tree = dict(name=os.path.basename(path), children=[])
    try: lst = os.listdir(path)
    except OSError:
        pass #ignore errors
    else:
        for name in lst:
            fn = os.path.join(path, name)
            if os.path.isdir(fn):
                tree['children'].append(make_tree(fn))
            else:
                tree['children'].append(dict(name=name))
    return tree
在my html page files.html中:

<title>Path: {{ tree.name }}</title>
<h1>{{ tree.name }}</h1>

<div class="accordion-heading" >
    <div class="accordion-toggle" >
         <a data-toggle="collapse"  data-target="#files_list" href="#files_list">
            <b>

<ul>
{%- for item in tree.children recursive %}
 <div class="well well-sm">   <li>{{ item.name }} </div>
    {%- if item.children -%}
        <ul>{{ loop(item.children) }}</ul>
    {%- endif %}</li>
{%- endfor %}
</ul>

 </b></div>
         </a>
    </div>

请提供任何帮助???

您可能可以阅读文件的内容,并将其传递到
中的模板,使树的功能如下:

def make_tree(path):
    tree = dict(name=os.path.basename(path), children=[])
    try: lst = os.listdir(path)
    except OSError:
        pass #ignore errors
    else:
        for name in lst:
            fn = os.path.join(path, name)
            if os.path.isdir(fn):
                tree['children'].append(make_tree(fn))
            else:
                with open(fn) as f:
                    contents = f.read()
                tree['children'].append(dict(name=name, contents=contents))
    return tree
只需将
{{item.contents}}
添加到要显示文件内容的位置。例如,这里:

{%- for item in tree.children recursive %}
     <div class="well well-sm">
        <li>
            {{ item.name }}
            <pre>{{ item.contents }}</pre>
            {%- if item.children -%}
                <ul>{{ loop(item.children) }}</ul>
            {%- endif %}
        </li>
    </div>
{%- endfor %}
{%-for tree.children recursive%}
  • {{item.name} {{item.contents} {%-if item.children-%}
      {{loop(item.children)}}
    {%-endif%}
  • {%-endfor%}

    这有点难看,但它应该显示正确的数据。

    欢迎使用堆栈溢出!你所说的文件内容是什么意思?比如图像如果是
    .jpg
    ,源代码如果是
    .py
    ?是的,对不起,我指的是文本文件。我将编辑我的问题。谢谢您需要将文件内容存储在数组或字典中。您正在遍历所有文件内容,只需重新指定file_contents变量。传递到模板的所有内容都是最后一个文件内容。因此,要么追加到数组中,要么添加到字典中。然后你可以在你的模板中循环。Ilya谢谢!这管用!谢谢你的帮助!非常感谢:)很高兴我能帮忙:)
    {%- for item in tree.children recursive %}
         <div class="well well-sm">
            <li>
                {{ item.name }}
                <pre>{{ item.contents }}</pre>
                {%- if item.children -%}
                    <ul>{{ loop(item.children) }}</ul>
                {%- endif %}
            </li>
        </div>
    {%- endfor %}