列出目录的内容os.path.isfile文件()和操作系统路径.isdir()在Python中

2024-04-26 04:37:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要用Python创建一个web服务器来列出一个目录的内容(文件和目录),但结果是只列出了文件,在我做的测试中,我列出了“/etc”,但问题是它没有显示/etc中存在的目录,只是几个/etc,然后它正确地列出了文件,有人能帮我吗??你知道吗

import web
import os

urls = (
'/(.*)', 'hello'
)
app = web.application(urls, globals())

class hello:
    def GET(self, path):

        path = '/etc/'+path
        lista = '<html> <body>'
        caminhos = [os.path.join(path, nome) for nome in os.listdir(path)]
        for nome in caminhos:
            lista = lista+path+'<br>'
        arquivos = [arq for arq in caminhos if os.path.isfile(arq)]
           for arq in arquivos:
               lista = lista+arq+ '<br>'
           lista = lista+'</body> </html>'
           return lista


if __name__ == "__main__":
    app.run()

Tags: 文件pathinimport目录webhellofor

热门问题