Search code examples
azure-filesazure-storage-files

differences between get_directory_client and get_subdirectory_client for azure file share


Azure file share Python SDKs have two similar methods get_directory_client and get_subdirectory_client. It seems both are interacting with directories. But do we need two methods to perform the same task?


Solution

  • get_directory_client is to get the root directory, and get_subdirectory_client is to get the subdirectories of the current directory.

    As you can see from the document, you must get the ShareClient object first. At this time, you can only call get_directory_client to get the root directory, and then you will get the ShareDirectoryClient object. At this time, if you want to get the subdirectory , You can only call the get_subdirectory_client method.

    You can also refer to the description of the file share client to understand the difference:

    enter image description here

    ==========================update======================

        connection_string = "<your-connection-string>"
        service = ShareServiceClient.from_connection_string(conn_str=connection_string)
        share = service.get_share_client("<your-file-share-name>")
        my_files = []
        for item in share.list_directories_and_files():
            my_files.append(item)
            if item["is_directory"]:
                for item2 in share.get_directory_client(item["name"]).list_directories_and_files():
                    my_files.append(item)
                    for item3 in share.get_directory_client(item["name"]).get_subdirectory_client(item2["name"]).list_directories_and_files():
                        my_files.append(item3)
            else:
                my_files.append(item)
        print(my_files)
    

    You can refer to this official documentation.