1

我正在尝试用一些文本编写文本文件并在数据块中加载相同的文本文件,但我收到错误

代码

#write a file to DBFS using Python I/O APIs
with open("/dbfs/FileStore/tables/test_dbfs.txt", 'w') as f:
  f.write("Apache Spark is awesome!\n")
  f.write("End of example!")

# read the file
with open("/dbfs/tmp/test_dbfs.txt", "r") as f_read:
  for line in f_read:
    print(line)

错误 FileNotFoundError:[Errno 2] 没有这样的文件或目录:'/dbfs/FileStore/tables/test_dbfs.txt'

4

1 回答 1

0

/dbfs挂载不适用于 DBR >= 7.x 的社区版 - 这是一个已知限制。

要解决此限制,您需要使用驱动程序节点上的文件并使用dbutils.fs.cp命令 ( docs ) 上传或下载文件。所以你的写作将如下所示:

#write a file to local filesystem using Python I/O APIs
with open("'file:/tmp/local-path'", 'w') as f:
  f.write("Apache Spark is awesome!\n")
  f.write("End of example!")
# upload file to DBFS
dbutils.fs.cp('file:/tmp/local-path', 'dbfs:/FileStore/tables/test_dbfs.txt')

从 DBFS 读取将如下所示:

# copy file from DBFS to local file_system
dbutils.fs.cp('dbfs:/tmp/test_dbfs.txt', 'file:/tmp/local-path')
# read the file locally
with open("/tmp/local-path", "r") as f_read:
  for line in f_read:
    print(line)
于 2021-05-27T06:47:10.133 回答