python os.walk达到一定水平 [英] python os.walk to certain level
问题描述
我想构建一个使用一些基本代码来通读文件夹的程序,并告诉我该文件夹中有多少个文件. 目前,这是我的操作方式:
I want to build a program that uses some basic code to read through a folder and tell me how many files are in the folder. Here is how I do that currently:
import os
folders = ['Y:\\path1', 'Y:\\path2', 'Y:\\path3']
for stuff in folders:
for root, dirs, files in os.walk(stuff, topdown=True):
print("there are", len(files), "files in", root)
这很好用,直到主"文件夹中有多个文件夹,因为由于不良的文件夹/文件管理,它可能会返回很长的,混乱的文件列表.因此,我最多只想转到第二级.例如:
This works great until there are multiple folders inside the "main" folder as it can return a long, junky list of files due to poor folder/file management. So I would like to go only to the second level at most. example:
Main Folder
---file_i_want
---file_i_want
---Sub_Folder
------file_i_want <--*
------file_i want <--*
------Sub_Folder_2
---------file_i_dont_want
---------file_i_dont_want
我知道如何仅使用break
和del dirs[:]
从这篇文章和也是这篇文章.
I know how to go to only the first level with a break
and with del dirs[:]
taken from this post and also this post.
import os
import pandas as pd
folders = ['Y:\\path1', 'Y:\\path2', 'Y:\\path3']
for stuff in folders:
for root, dirs, files in os.walk(stuff, topdown=True):
print("there are", len(files), "files in", root)
del dirs[:] # or a break here. does the same thing.
但是无论我进行何种搜索,我都无法找到如何深入两层的方法.我可能只是不了解它的其他内容或其他内容?我在想类似del dirs[:2]
的方法,但无济于事.有人可以指导我或向我解释如何实现这一目标吗?
But no matter my searching I can't find out how to go two layers deep. I may just not be understanding the other posts on it or something? I was thinking something like del dirs[:2]
but to no avail. Can someone guide me or explain to mehow to accomplish this?
推荐答案
您可以这样做:
depth = 2
# [1] abspath() already acts as normpath() to remove trailing os.sep
#, and we need ensures trailing os.sep not exists to make slicing accurate.
# [2] abspath() also make /../ and ////, "." get resolved even though os.walk can returns it literally.
# [3] expanduser() expands ~
# [4] expandvars() expands $HOME
stuff = os.path.abspath(os.path.expanduser(os.path.expandvars(stuff)))
for root,dirs,files in os.walk(stuff):
if root[len(stuff):].count(os.sep) < depth:
for f in files:
print(os.path.join(root,f))
键是:if root[len(stuff):].count(os.sep) < depth
它从root
中删除了stuff
,因此结果是相对于stuff
的.只需计算文件分隔符的数量即可.
It removes stuff
from root
, so result is relative to stuff
. Just count the number of files separators.
深度操作类似于Linux中的find
命令,即-maxdepth 0
表示什么都不做,-maxdepth 1
仅扫描第一级的文件,并且-maxdepth 2
扫描包含在子目录中的文件.
The depth acts like find
command found in Linux, i.e. -maxdepth 0
means do nothing, -maxdepth 1
only scan files in first level, and -maxdepth 2
scan files included sub-directory.
当然,它仍然会扫描整个文件结构,但是除非它很深,否则它将起作用.
Of course, it still scans the full file structure, but unless it's very deep that'll work.
另一种解决方案是仅递归地使用os.listdir
(带有目录检查),并具有最大的递归级别,但是如果您不需要它,则会有些棘手.由于不那么困难,因此这里是一个实现:
Another solution would be to only use os.listdir
recursively (with directory check) with a maximum recursion level, but that's a little trickier if you don't need it. Since it's not that hard, here's one implementation:
def scanrec(root):
rval = []
def do_scan(start_dir,output,depth=0):
for f in os.listdir(start_dir):
ff = os.path.join(start_dir,f)
if os.path.isdir(ff):
if depth<2:
do_scan(ff,output,depth+1)
else:
output.append(ff)
do_scan(root,rval,0)
return rval
print(scanrec(stuff)) # prints the list of files not below 2 deep
注意:os.listdir
和os.path.isfile
执行2次stat
调用,因此不是最佳选择.在Python 3.5中,使用os.scandir
可以避免重复调用.
Note: os.listdir
and os.path.isfile
perform 2 stat
calls so not optimal. In Python 3.5, the use of os.scandir
could avoid that double call.
这篇关于python os.walk达到一定水平的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!