How to check file exists in databricks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2019 12:34 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2019 09:02 PM
There might be other ways to do this, but this works.
@Simhadri Raju
Basically,
I use dbutils.fs.head here, but anything that throws an exception if it fails to find the file would work.
So I go to read the first byte of the file with
dbutils.fs.head(arg1,1)
If that throws an exception I return False
If that succeeds I return True.
Put that in a function, call the function with your filename and you are good to go.
Full code here
## Function to check to see if a file existsdef fileExists (arg1): try: dbutils.fs.head(arg1,1) except: return False; else: return True;
Calling that function with your filenameilename = <pathtoyourfile>
if(fileExists(filename)): print("Yes it exists");
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2019 09:03 PM
Oh that code did not render well,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2019 01:49 AM
Here's a Python solution:
def file_exists(path):
try:
dbutils.fs.ls(path)
return True
except Exception as e:
if 'java.io.FileNotFoundException' in str(e):
return False
else:
raise
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2020 09:32 AM
Very many thanks to @zerogjoe for his elegant answer, which works perfectly for Databricks formatted file paths.
To make this a little more robust and allow for filesystem api paths (that can be used with os, glob etc and start with "/dbfs") I've added a few lines of code.
def exists(path): """ Check for existence of path within Databricks file system. """
if path[:5] == "/dbfs":
import os
return os.path.exists(path)
else:
try:
dbutils.fs.ls(path)
return True
except Exception as e:
if 'java.io.FileNotFoundException' in str(e):
return False
else:
raise
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2021 12:26 PM
If you are looking for a scala solution, here it is:
def pathExists(tablePath: String): Boolean = {
try{
dbutils.fs.ls(tablePath)
return true
} catch {
case e: java.io.FileNotFoundException => println("Given path cannot be located")
return false
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2023 03:15 AM
You can do so by running the snippet below, which uses the new [databricks python SDK](https://github.com/databricks/databricks-sdk-py/😞
Install the package:
`pip install databricks-sdk`
Python snippet:
```python
from databricks.sdk import WorkspaceClient
# Remember to change the arguments below
w_client = WorkspaceClient(host="my_host", token="my_db_tokens")
# add an absolute path
dbfs_path_exist = w_client.dbfs.exists('/dbfs_my_path')
```
Hope it helps 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2024 04:31 PM
How to check if a file exists in DBFS?
Let's write a Python function to check if the file exists or not
-------------------------------------------------------------
def file_exists(path):
try:
dbutils.fs.ls(path)
return True
except Exception as e:
if 'java.io.FileNotFoundException' in str(e):
return False
else:
raise
Result = file_exists("dbfs:/Repos/")
print(Result)
"True" represents that path exists

