在 Databricks 中访问工作区文件的正确方法是什么

what is the correct way to access a workspace file in databricks

提问人:jdhao 提问时间:11/17/2023 更新时间:11/17/2023 访问量:52

问:

我的 databricks 运行时版本是 10.4 LTS。我正在尝试使用 python 中的方法访问工作区文件。我尝试了多种不同的方法,但它们都失败了。 假设我的工作区文件路径是 ,我尝试了以下路径:open()/Workspace/Users/<user-email>/my_result.json

  • 不带任何前缀,:失败/Workspace/Users/<user-email>/my_result.json
  • 添加 , : 失败/dbfs/dbfs/Workspace/Users/<user-email>/my_result.json
  • 添加 , : 失败/file/file/Workspace/Users/<user-email>/my_result.json

是我的路径格式错误,还是我们无法使用 Python 的方法访问工作区下的文件?open()

Databricks Azure-Databricks

评论


答:

1赞 JayashankarGS 11/17/2023 #1

根据这些文档(12),工作区文件或资产可用于 Databricks Runtime 11.2 及更高版本。

使用 Databricks Runtime 11.2 及更高版本,可以在 Azure Databricks 工作区中创建和管理源代码文件,然后根据需要将这些文件导入笔记本。

使用不带前缀的路径是正确的方法。它在 Runtime 11.2 及更高版本中工作正常。

file_path = "/Workspace/Users/[email protected]/outdata.json"

try:
    with open(file_path, 'r') as file:
        content = file.read()
        print(content)
except Exception as e:
    print(f"Error: {e}")

输出:

enter image description here

因此,需要使用 Databricks Runtime 11.2 及更高版本,或将文件上传到 .打开文件时,在路径前面加上 。.jsondbfs/dbfs/path_to_file/

评论

0赞 jdhao 11/17/2023
感谢您的回复!在屏幕截图中运行代码的 Databricks Runtime 版本是什么?
0赞 JayashankarGS 11/17/2023
我的是 13.3。我已经检查了 10.4,它只是没有给出这样的文件或目录。您可以通过列出文件夹本身进行检查。.您将在 11.2 及更高版本中找到文件夹。%fs ls file:/Workspace/Users