我需要一些提示并帮助解决我的问题。我想在一个目录上的jupyter笔记本中创建一个下拉菜单。在这个目录里面是带有png的目录,有些目录是带有png的子目录。现在我想得到所有dir的列表,里面有png。但不是dir的里面的dir。 ATM我得到每个目录,但这是错误的原因我只能使用dir与png的内部。那是我的代码atm:
dir_path = 'Learning set/Calanus hyp/Chyp 1/'
path = 'Learning set/'
for root, dirs, files in os.walk(path):
for name in dirs:
if ([os.path.isfile(os.path.join(path, f)) for f in os.listdir(path)]) == True:
namelist += [os.path.join(root,name)]
def get_and_plot(b):
clear_output()
global dir_path
dir_path_new=test.value+"/"
dir_path=dir_path_new
global counter
counter=0
execute()
test.observe(get_and_plot, names='value')
举个例子:我有一个目录“Learing Set”,想要一个下拉列表。但是下拉菜单向我显示了dir的Bubble,Bubble1,Bubble2等.Dir的BubbleX里面有pngs。多数民众赞成。但是泡泡目录只是实现了其他的BubbleX目录。我该怎么做才能获得所有最后一个子目录的列表?
root
将是步行期间的当前目录。如果其任何文件符合您的条件,请将root
添加到列表(或其他一些容器)中。
dirs_i_want = []
for root, dirs, files in os.walk(path):
if any(fyle.endswith('png') for fyle in files:
dirs_i_want.append(root)
如果您希望目录仅包含符合条件的文件,请在条件语句中使用all
。
...
if all(fyle.endswith('png') for fyle in files:
...