问题描述
我正在尝试用一些文本编写文本文件并在数据块中加载相同的文本文件,但出现错误
#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'
解决方法
/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)