使用Python仅获取Azure Data Lake目录中的子文件夹名称列表

2024-10-02 18:19:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我在名为infaqa的Azure data lake容器中有数据,infaqa中有目录,路径如下: `infqa/EIM//Sales/Raw/APXTConga4\u Composer\u Setting\u\u mdt、infqa/EIM//Sales/Raw/Account等

我正在使用Azure笔记本和类似blobserviceclient to list blobs的库,但我看到所有子文件夹列表和子文件夹都在列表中。其中,我只在下面显示的输出中查找类似['APXTConga4__Composer_Setting__mdt','Account'...]的列表中的子文件夹名称

    Input:
        blobPrefix = "/EIM/Sales/Raw/"     
        mylist=[]
        objects=[]
        blob_list = container_client.list_blobs(blobPrefix)
        for blob in blob_list:
            mylist.append(blob.name)
            print(blob.name)
    
    Ouptut:
    EIM/Sales/Raw/APXTConga4__Composer_Setting__mdt
    EIM/Sales/Raw/APXTConga4__Composer_Setting__mdt/2020
    EIM/Sales/Raw/APXTConga4__Composer_Setting__mdt/2020/12
    EIM/Sales/Raw/APXTConga4__Composer_Setting__mdt/2020/12/02
    EIM/Sales/Raw/Account
    EIM/Sales/Raw/Account/2020
    EIM/Sales/Raw/Account/2020/12
    EIM/Sales/Raw/Account/2020/12/02

Tags: 文件夹列表rawaccountazuresettingbloblist
1条回答
网友
1楼 · 发布于 2024-10-02 18:19:47

尝试使用此解决方案,我在我的系统中进行了尝试

我的文件夹结构类似于test容器和account是文件夹

1)test/account/main/sub1/
2)test/account/test1/sub2
3)test/account/test2/sub3


from azure.storage.blob import BlobServiceClient
import os

source_key = 'Key'
source_account_name = 'Account Name'
block_blob_service = BlobServiceClient(
    account_url=f'https://{source_account_name}.blob.core.windows.net/', credential=source_key)
source_container_client = block_blob_service.get_container_client(
    'Container name')
result=[]
allfolders=[]
generator =source_container_client.list_blobs("account")

for file in source_container_client.walk_blobs('account/', delimiter='/'):
    print(file.name)
    text=file.name
    result.append(text)
for data in result:
    
    allfolders.append(data.replace("account/",""))
print(allfolders)
for res in allfolders:
    print(res)

输出

存储帐户中的文件夹结构

enter image description here

enter image description here

能够获取所有子文件夹名称

enter image description here

相关问题 更多 >