Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获取给定目录中的子目录列表_Python_Python 2.7 - Fatal编程技术网

Python 获取给定目录中的子目录列表

Python 获取给定目录中的子目录列表,python,python-2.7,Python,Python 2.7,我很难获得列出给定目录中所有目录/子目录的xml结构。我发现在我的问题中使用递归比平常要难一点。我的目录中可能有10000个文件,所以检查每一个内容,看看它是否是一个目录将是昂贵的,而且构建xml已经需要很长时间。我只想为目录构建xml 我知道linux有一些命令,比如find-键入d以列出存在的目录(而不是文件)。如何在python中实现这一点 提前感谢。已经区分了文件和目录: def find_all_dirs(root='.'): for path,dirs,files in os

我很难获得列出给定目录中所有目录/子目录的xml结构。我发现在我的问题中使用递归比平常要难一点。我的目录中可能有10000个文件,所以检查每一个内容,看看它是否是一个目录将是昂贵的,而且构建xml已经需要很长时间。我只想为目录构建xml

我知道linux有一些命令,比如find-键入d以列出存在的目录(而不是文件)。如何在python中实现这一点

提前感谢。

已经区分了文件和目录:

def find_all_dirs(root='.'):
    for path,dirs,files in os.walk(root):
        for d in dirs:
            yield os.path.join(path, d)

只需一个目录

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("."))

这是我在搜索和尝试不同事物后得到的解决方案。我并不是说这种查找目录中每一个内容的方法比以前快,但它实际上生成的结果要快得多(当目录包含1000个文件时,差异是显而易见的)

导入操作系统
导入子流程
从xml.sax.saxutils导入quoteattr作为xml
def DIRASLESXML(路径):
结果='\n'.格式(xml'u quoteattr('dir')、xml'u quoteattr(os.path.basename(path))、xml'u quoteattr(path))
list=subprocess.Popen(['find',path',maxdepth',1','-type',d'],stdout=subprocess.PIPE,shell=False)。communicate()[0]
output_list=list.splitlines()
如果len(输出列表)=1:
结果='\n'.格式(xml'u quoteattr('leaf'u dir')、xml'u quoteattr(os.path.basename(path))、xml'u quoteattr(path))
对于输出列表[1:]中的项目:
结果+='\n'.连接(''+行表示DirAsLessXML(item).split('\n'))
结果+='\n'
返回结果

@phihagos.walk会自动循环。我之所以要避免这种情况,是因为我必须创建给定目录中存在的子目录的xml对不起,“递归本身”是什么意思?如果您只是创建一个文件,那么它不应该对目录树产生影响。但是,即使您要创建一个目录,您也可以简单地将整个树存储在一个带有
dirs=list(find\u all\u dirs())
的变量中,然后对该列表进行操作。@phihag-I试图通过递归本身构建XML。是的,我可以将整棵树存储在一个变量中,然后用它创建一个xml,@spiralx不赞成使用os.path.isdir,不想检查每一个内容,看它是否是一个目录,因为目录中可能包含10000个文件。我想,这大概和使用标准库的速度一样快,没有只获取目录的命令-它将如何工作?您可以使用
子流程
模块来执行二进制文件,但我不知道是否会更快。
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