Display content of files in a particular directory using Flask
<p>I am implementing an app using Flask and I am trying to display the content of text files I put in log directory. So i have done this:</p>

<pre><code>@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
</code></pre>

<p>in my html page files.html:</p>

<pre><code>&lt;title&gt;Path: {{ tree.name }}&lt;/title&gt;
&lt;h1&gt;{{ tree.name }}&lt;/h1&gt;

&lt;div class="accordion-heading" &gt;
    &lt;div class="accordion-toggle" &gt;
         &lt;a data-toggle="collapse"  data-target="#files_list" href="#files_list"&gt;
            &lt;b&gt;

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

 &lt;/b&gt;&lt;/div&gt;
         &lt;/a&gt;
    &lt;/div&gt;
</code></pre>

<p>This only displays the names of the files inside my log directory but I am not able to display the content of the files.
I thought maybe I can use something like:</p>

<pre><code>import fnmatch
def display_files():
    for dirpath, dirs, files in os.walk('log'):
        for filename in fnmatch.filter(files, '*.*'):
        with open(os.path.join(dirpath, filename)) as f:
            file_contents = f.read().strip()
            print file_contents

    return render_template('files.html',title="index",file_contents=file_contents) 
</code></pre>

<p>Any help please??</p>