问题描述
Azure 文件共享 Python SDK 有两个类似的方法 get_directory_client
和 get_subdirectory_client
。似乎两者都在与目录交互。但是我们是否需要两种方法来执行相同的任务?
解决方法
get_directory_client
是获取根目录,get_subdirectory_client
是获取当前目录的子目录。
从文档中可以看出,必须先获取ShareClient对象。这时候只能调用get_directory_client获取根目录,然后才能获取到ShareDirectoryClient对象。这时候如果要获取子目录,只能调用get_subdirectory_client方法。
您也可以参考description of the file share client了解区别:
==========================更新================== ==
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)
你可以参考这个official documentation。