Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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 2.7 在Python 2.7中使用“scandir”_Python 2.7_List_Subdirectory_Scandir - Fatal编程技术网

Python 2.7 在Python 2.7中使用“scandir”

Python 2.7 在Python 2.7中使用“scandir”,python-2.7,list,subdirectory,scandir,Python 2.7,List,Subdirectory,Scandir,我希望快速建立一个目录中所有文件夹和子文件夹的列表,而无需遍历每个子文件夹中的文件。最后一点就是为什么我不使用os.walk。为此,我遵循了一个建议使用scandir的示例: import scandir ds = scandir.scandir(datafolder) 这将生成一个scandir.ScandirIterator对象。我该如何看待这个对象,或者将其视为一个列表?你又回到了原点,我的朋友!要查看迭代器对象中的“数据”,必须进行迭代。我想这样的事情会奏效,或者至少让你走上正确的道路

我希望快速建立一个目录中所有文件夹和子文件夹的列表,而无需遍历每个子文件夹中的文件。最后一点就是为什么我不使用os.walk。为此,我遵循了一个建议使用
scandir
的示例:

import scandir
ds = scandir.scandir(datafolder)

这将生成一个
scandir.ScandirIterator对象
。我该如何看待这个对象,或者将其视为一个列表?

你又回到了原点,我的朋友!要查看迭代器对象中的“数据”,必须进行迭代。我想这样的事情会奏效,或者至少让你走上正确的道路(双关语)


这直接回答了我的问题,谢谢!我希望
scandir
会给我
datafolder
中的子文件夹,但我希望得到所有子目录。我怎么能这样做呢?@AaronJPung有点讽刺地说,
scandir.walk
scandir.walk
os.walk
在功能上似乎非常相似,但是
scandir
模块声称速度更快,因此使用它可能比
os.walk
import scandir
ds = scandir.scandir(datafolder)    
print [x for x in ds]