在Python中创建一个树形目录列表 [英] Create a tree-style directory listing in Python

查看:176
本文介绍了在Python中创建一个树形目录列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  ./ rootdir $ b $我想列出目录和文件(recursivley) b ./file1.html 
./subdir1
./file2.html
./file3.html
./subdir2
./file4.html

现在我可以列出目录和文件了(从这里)。但是我想用下面的格式和ORDER(这对我所做的事情非常重要)列出它。

  / rootdir / 
/rootdir/file1.html
/ rootdir / subdir1 /
/rootdir/subdir1/file2.html
/rootdir/subdir1/file3.html
/ rootdir / subdir2 /
/rootdir/file4.html

我不在乎如何得到编辑:添加下面的代码。


$ b 编辑:添加下面的代码。 b
$ b

 #list books 
import os
import sys

lstFiles = []
rootdir =/ srv / http / example / www / static / dev / library / books

将目录和文件附加到列表
中作为路径,目录和文件。 (rootdir):
#lstFiles.append(path +/)
lstFiles.append(path)
文件中的文件:
lstFiles.append(os.path .join(path,file))

#打开文件
f = open(sidebar.ht ml,w)
f.write(< ul>)

for lstFiles中的项目:
splitfile = os.path.split(item)
webpyPath = splitfile [0] .replace(/ srv / http / example / www,)
itemName = splitfile [1]
if item.endswith(/):
f.write('< li>< a href = \'+ webpyPath +/+ itemName +'\id = \directory \alt = \' + itemName +'\target = \viewer \>'+ itemName +'< / a>< / li> \\\
')
else:
f。 write('< li>< a href = \''+ webpyPath +/+ itemName +'\id = \file \alt = \'+ itemName +'\ target = \viewer \>'+ itemName +'< / a>< / li> \\\
')

f.write(< / ul> )
f.close()


解决方案

尝试以下操作:

pre $ for path,dirs,os.walk中的文件(。):
打印路径
用于文件中的文件s:
print os.path.join(path,file)

你不需要从 dirs 打印条目,因为当你走路径的时候每个目录都会被访问,所以你稍后用打印路径


I am trying to list directories and files (recursivley) in a directory with python:

./rootdir
  ./file1.html
  ./subdir1
    ./file2.html
    ./file3.html
  ./subdir2
  ./file4.html

Now I can list the directories and files just fine (borrowed it from here). But I would like to list it in the following format and ORDER (which is very important for what I am doing.

/rootdir/
/rootdir/file1.html
/rootdir/subdir1/
/rootdir/subdir1/file2.html
/rootdir/subdir1/file3.html
/rootdir/subdir2/
/rootdir/file4.html

I don't care how it gets done. If I walk the directory and then organize it or get everything in order. Either way, thanks in advance!

EDIT: Added code below.

# list books
import os
import sys

lstFiles = []
rootdir = "/srv/http/example/www/static/dev/library/books"

# Append the directories and files to a list
for path, dirs, files in os.walk(rootdir):
    #lstFiles.append(path + "/")
    lstFiles.append(path)
    for file in files:
        lstFiles.append(os.path.join(path, file))

# Open the file for writing
f = open("sidebar.html", "w")
f.write("<ul>")

for item in lstFiles:
    splitfile = os.path.split(item)
    webpyPath = splitfile[0].replace("/srv/http/example/www", "")
    itemName = splitfile[1]
    if item.endswith("/"):
        f.write('<li><a href=\"' + webpyPath + "/" + itemName + '\" id=\"directory\" alt=\"' + itemName + '\" target=\"viewer\">' + itemName + '</a></li>\n')
    else:
        f.write('<li><a href=\"' + webpyPath + "/" + itemName + '\" id=\"file\" alt=\"' + itemName + '\" target=\"viewer\">' + itemName + '</a></li>\n')

f.write("</ul>")
f.close()

解决方案

Try the following:

for path, dirs, files in os.walk("."):
    print path
    for file in files:
        print os.path.join(path, file)

You do not need to print entries from dirs because each directory will be visited as you walk the path, so you will print it later with print path.

这篇关于在Python中创建一个树形目录列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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