python烧瓶浏览目录与文件 [英] python flask browsing through directory with files

查看:119
本文介绍了python烧瓶浏览目录与文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的代码从来没有像正常工作,因为字符串之间的奇怪附加发生。



另外,我不知道如何执行一种检查路径是文件还是文件夹。



是我的Flask app.route:

  @ app.route('/ files',defaults = {'folder':None, 'sub_folder':None},methods = ['GET'])
@ app.route('/ files /< folder>',defaults = {'sub_folder':None},methods = ['GET' ])
@ app.route('/ files /< folder> /< sub_folder>',methods = ['GET'])

def files(folder,sub_folder):
basedir ='files /'
directory =''

if folder!= None:
directory = directory +'/'+ folder

if sub_folder!= None:
directory = directory +'/'+ sub_folder

files = os.listdir(basedir +目录)

return render_template('files.html',files = files,directory = basedir + directory,currdir = directory)

这里是我的html模板,如果有人能给我一些指针,将不胜感激!

pre > <身体GT;
< h2>文件{{currdir}}< / h2> < / BR>
{%name for files:%}
{%endfor%}
< / body> s.html',files = files,directory = basedir + directory,currdir = directory)
pre

解决方案

docs 链接)优于硬编码所有不同的可能路径结构。

b
$ b

os.path.exists 可用于检查路径是否有效, os.path.isfile os.path.isdir 分别用于检查路径是文件还是目录。



端点:

  @ app.route('/',defaults = {'req_path':''} )
@ app.route('/< path:req_path>')
def dir_listing(req_path):
BASE_DIR ='/ Users / vivek / Desktop'

#连接基础和请求的路径
abs_path = os.path.join(BASE_DIR,req_path)

#如果路径不返回如果不存在,则存在
os.path.exists(abs_path):
返回abort(404)

#检查路径是否为文件并且服务
if os。 path.isfile(abs_path):
返回send_file(abs_path)

#显示目录内容
files = os.listdir(abs_path)
返回render_template('files。 html',files = files)

模板:

 < UL> 
{%file for file in files%}
< li>< a href ={{file}}> {{file}}< / a>< / li>
{%endfor%}
< / ul>

注意: abort send_file 函数是从flask导入的。


Is it possible to use flask to browse through a directory with files?

My code never seems to work correctly as weird appending between strings happens.

Also I don`t know how to implement a kind of check whether the path is a file or a folder.

Here is my Flask app.route:

@app.route('/files', defaults={'folder': None,'sub_folder': None}, methods=['GET'])
@app.route('/files/<folder>', defaults={'sub_folder': None}, methods=['GET'])
@app.route('/files/<folder>/<sub_folder>', methods=['GET'])

    def files(folder,sub_folder):
        basedir = 'files/'
        directory = ''

        if folder != None:
            directory = directory + '/' + folder

        if sub_folder != None:
            directory = directory + '/' + sub_folder

        files = os.listdir(basedir + directory)

        return render_template('files.html',files=files,directory=basedir + directory,currdir=directory)

and here is my html template, if anyone could give me some pointers it would be greatly appreciated!

<body>
    <h2>Files {{ currdir }}</h2> </br>
    {% for name in files: %}
        <A HREF="{{ directory }}{{ name }}">{{ name }}</A> </br></br>
    {% endfor %}
</body>s.html',files=files,directory=basedir + directory,currdir=directory)

解决方案

A path converter (docs link) in the url structure is better than hardcoding all the different possible path structures.

os.path.exists can be used to check if the path is valid and os.path.isfile and os.path.isdir for checking if the path is a file or a directory, respectively.

Endpoint:

@app.route('/', defaults={'req_path': ''})
@app.route('/<path:req_path>')
def dir_listing(req_path):
    BASE_DIR = '/Users/vivek/Desktop'

    # Joining the base and the requested path
    abs_path = os.path.join(BASE_DIR, req_path)

    # Return 404 if path doesn't exist
    if not os.path.exists(abs_path):
        return abort(404)

    # Check if path is a file and serve
    if os.path.isfile(abs_path):
        return send_file(abs_path)

    # Show directory contents
    files = os.listdir(abs_path)
    return render_template('files.html', files=files)

Template:

<ul>
    {% for file in files %}
    <li><a href="{{ file }}">{{ file }}</a></li>
    {% endfor %}
</ul>

Note: abort and send_file functions were imported from flask.

这篇关于python烧瓶浏览目录与文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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