gpt4 book ai didi

azure - 列出位于 Azure Blob 存储中的所有文件名

转载 作者:行者123 更新时间:2023-12-03 02:11:40 25 4
gpt4 key购买 nike

我想在 Databricks 中列出位于 Azure Blob 存储中的所有文件名。
我的Azure Blob存储的结构如下:

aaa
<br/>------bbb
<br/>------------bbb1.xml
<br/>------------bbb2.xml
<br/>------ccc
<br/>------------ccc1.xml
<br/>------------ccc2.xml
<br/>------------ccc3.xml

如果我这样做:

dbutils.fs.ls('wasbs://<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="760e0e0e360e0e0e58141a1914581519041358011f181219010558181302" rel="noreferrer noopener nofollow">[email protected]</a>/aaa')

仅列出子文件夹 bbb 和 ccc,如下所示:

[FileInfo(path='wasbs://<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="90e8e8e8d0e8e8e8bef2fcfff2bef3ffe2f5bee7f9fef4ffe7e3befef5e4" rel="noreferrer noopener nofollow">[email protected]</a>/aaa/bbb/', name='bbb/', size=0), 
FileInfo(path='wasbs://<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="89f1f1f1c9f1f1f1a7ebe5e6eba7eae6fbeca7fee0e7ede6fefaa7e7ecfd" rel="noreferrer noopener nofollow">[email protected]</a>/aaa/ccc/', name='ccc/', size=0)]

我想深入到最后一个子文件夹以查看位于 aaa 中的所有文件名:bbb1.xmlbbb2.xmlccc1.xmlccc2.xmlccc3.xml

如果我这样做:

dbutils.fs.ls('wasbs://<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aed6d6d6eed6d6d680ccc2c1cc80cdc1dccb80d9c7c0cac1d9dd80c0cbda" rel="noreferrer noopener nofollow">[email protected]</a>/aaa/*')

由于路径无法参数化而发生错误。

有什么想法可以在 Databricks 中执行此操作吗?

最佳答案

dbutils.fs.ls 不支持通配符,这就是您收到错误的原因。你有几个选择:

  1. 使用Python SDK for Azure blob storage to list files - 它可能比使用递归 dbutils.fs.ls 更快,但您需要设置身份验证等。

  2. 您可以使用这样的函数对dbutils.fs.ls进行递归调用,但性能不是很好:

def list_files(path, max_level = 1, cur_level=0):
"""
Lists files under the given path, recursing up to the max_level
"""
d = dbutils.fs.ls(path)
for i in d:
if i.name.endswith("/") and i.size == 0 and cur_level < (max_level - 1):
yield from list_files(i.path, max_level, cur_level+1)
else:
yield i.path
  • 您可以使用 Hadoop API 访问容器中的文件,similar to this answer .
  • 关于azure - 列出位于 Azure Blob 存储中的所有文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73163804/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com