[英]Python3 listing all files/directories on same level as certain folder name
I have a path like this: 我有这样一条路:
/my/path/to/important_folder
on the same level, I have other files and folders that I want to list when I reach the same level as important_folder
. 在同一水平上,我还有其他的文件和文件夹,我想,当我达到同一水平列出
important_folder
。
My folder could be deeper, so I need to traverse through the folders until I reach important_folder
keep searching. 我的文件夹可能更深,因此我需要遍历这些文件夹,直到到达
important_folder
继续搜索。 Once found, list all files/folders in that same level. 找到后,列出同一级别的所有文件/文件夹。
How can I achieve this? 我该如何实现?
With os.walk
, you can do this: 使用
os.walk
,您可以执行以下操作:
import os
for path, dirs, files in os.walk('/my/path'):
if path == 'important_folder':
for name in dirs + files:
print(os.path.join(path, name))
Or you can use glob.iglob
with recursive=True
: 或者,您可以将
glob.iglob
与recursive=True
:
import glob
for name in glob.iglob('/my/path/**/important_folder/*', recursive=True):
print(name)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.