繁体   English   中英

如何使用 Python 从 Azure Data Lake Storage Gen2 检索所有目录路径?

[英]How do I retrieve all directory paths from Azure Data Lake Storage Gen2 using Python?

我正在尝试使用此处提到的方法检索 Azure Data Lake Storage Gen2 中目录的所有路径。 这是我的代码:

# Connect to account
def initialize_storage_account_ad(storage_account_name, client_id, client_secret, tenant_id):
    
    try:  
        global service_client

        credential = ClientSecretCredential(tenant_id, client_id, client_secret)

        service_client = DataLakeServiceClient(account_url="{}://{}.dfs.core.windows.net".format(
            "https", storage_account_name), credential=credential)
    
    except Exception as e:
        print(e)

# List Directory Contents
def list_directory_contents():
    try:
        
        file_system_client = service_client.get_file_system_client(file_system="my-file-system")

        paths = file_system_client.get_paths(path="my-directory")

        for path in paths:
            print(path.name + '\n')

    except Exception as e:
     print(e)

使用FileSystemClient.get_paths方法检索文件和目录的路径。

是否有有效的解决方法来检索或过滤目录路径?

请指教。

get_paths返回一个生成器来列出指定文件系统下的路径(可以是文件或目录),其中还包含每个路径的属性。

path.is_directory == True有助于区分目录和文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM