How do I check if there is already exists or not in databricks
dir = "/mnt/published/omega/omega_output"
if(dbutils.fs.exists(dir)):
print("dir exists")
else:
print("dir does not exists")
This code throws me error as
'FSHandler' object has no attribute 'exists'
There is no exists
function in the dbutils.fs
. There are few approaches to solve this:
/dbfs
to the path:import os
dir = '/mnt/....'
if os.path.exists(f"/dbfs{dir}"):
....
dbfs:/
, abfss:/
, ...URI = sc._gateway.jvm.java.net.URI
Path = sc._gateway.jvm.org.apache.hadoop.fs.Path
FileSystem = sc._gateway.jvm.org.apache.hadoop.fs.FileSystem
Configuration = sc._gateway.jvm.org.apache.hadoop.conf.Configuration
dir = "..."
fs = FileSystem.get(URI(dir), Configuration())
if fs.exists(Path(dir)):
...
def file_exists(dir):
try:
dbutils.fs.ls(dir)
except:
return False
return True