I have a file which contains a list of names stored in a simple text file. Each row contains one name. Now I need to pro grammatically append a new name to this file based on a users input. For the input itself I use DataBricks widgets - this is working just fine and I have the new name stored in a string object. Now I need to append this name to my file.
the file is mounted in the DataBricks File System (DBFS) under /mnt/blob/myNames.txt
when trying to read the file like this:
f = open("/mnt/blob/myNames.txt", "r")
print f
it returns an error "No such file or directory"
So I tried to wrap my new name into a dataframe and append it to the existing file but this also did not work as dataframe.write.save is designed to write into folders
what would be the most simple python could that I could use to append this new name to my file?
You can write and read files from DBFS with dbutils. Use the dbutils. fs. help() command in databricks to access the help menu for DBFS.
You can access the file system using magic commands such as %fs or %sh . You can also use the Databricks file system utility (dbutils. fs). Azure Databricks uses a FUSE mount to provide local access to files stored in the cloud.
If you use the Databricks Connect client library you can read local files into memory on a remote Databricks Spark cluster. See details here. The alternative is to use the Databricks CLI (or REST API) and push local data to a location on DBFS, where it can be read into Spark from within a Databricks notebook.
1. Explore the Databricks File System (DBFS) From Azure Databricks home, you can go to “Upload Data” (under Common Tasks)→ “DBFS” → “FileStore”. DBFS FileStore is where you create folders and save your data frames into CSV format.
You can open the file in append mode using 'a'
with open("/dbfs/mnt/sample.txt", "a") as f:
f.write("append values")
Now you can view the contents using
with open("/dbfs/mnt/sample.txt", "r") as f_read:
for line in f_read:
print(line)
Solution: Here
You can write and read files from DBFS with dbutils. Use the dbutils.fs.help() command in databricks to access the help menu for DBFS.
You would therefore append your name to your file with the following command:
dbutils.fs.put("/mnt/blob/myNames.txt", new_name)
You are getting the "No such file or directory" error because the DBFS path is not being found. Use dbfs:/ to access a DBFS path. This is how you should have read the file:
f = open("/dbfs/mnt/blob/myNames.txt", "r")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With