目标:迭代并写入根目录和存在的多个子目录中的所有 blob。
这是当前编写的代码:
for _file in container_client.walk_blobs(include='metadata'):
file_name = 'file.txt'
f = open(file_name, 'a+')
with open(file_name, 'r') as sample:
read_sample = sample.read()
if not re.search(_file.name, read_sample):
print('New One')
f.write(_file.name + os.linesep)
f.close()
- 上面的代码仅从根目录返回 blob 名称。
我能够从特定目录中提取 blob,但并非所有子目录都存在。
如何获取所有子目录中存在的 blob 名称?感谢您的帮助!
最佳答案
如果想列出一个目录下的blob,请引用以下代码
def walk_blob_hierarchy(prefix=""):
nonlocal depth
for item in container_client.walk_blobs(name_starts_with=prefix):
if isinstance(item, BlobPrefix):
walk_blob_hierarchy(prefix=item.name)
else:
print(item.name)
walk_blob_hierarchy(prefix="<folder name>")
关于python-3.x - 如何使用 python 在 Azure 云存储中迭代子目录中的所有 blob 名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68096215/