import os
def get_dirs(p):
p = os.path.abspath(p)
return [n for n in os.listdir(p) if os.path.isdir(os.path.join(p, n))]
print "\n".join(get_dirs("."))
import os
import subprocess
from xml.sax.saxutils import quoteattr as xml_quoteattr
def DirAsLessXML(path):
result = '<dir type ={0} name={1} path={2}>\n'.format(xml_quoteattr('dir'),xml_quoteattr(os.path.basename(path)),xml_quoteattr(path))
list = subprocess.Popen(['find', path,'-maxdepth', '1', '-type', 'd'],stdout=subprocess.PIPE, shell=False).communicate()[0]
output_list = list.splitlines()
if len(output_list) == 1:
result = '<dir type ={0} name={1} path={2}>\n'.format(xml_quoteattr('leaf_dir'),xml_quoteattr(os.path.basename(path)),xml_quoteattr(path))
for item in output_list[1:]:
result += '\n'.join(' ' + line for line in DirAsLessXML(item).split('\n'))
result += '</dir>\n'
return result
^{} 已经区分了文件和目录:
只有一个目录。。。在
这是我在寻找和尝试不同事物后得到的解决方案。我并不是说这种查找目录中每一个内容的方法要快得多,但它实际上产生的结果要快得多(当目录包含1000个文件时,差异是可见的)
相关问题 更多 >
编程相关推荐