5

I try to check if the path exists in Databricks using Python:

try:
  dirs = dbutils.fs.ls ("/my/path")
  pass
except IOError:
  print("The path does not exist")

If the path does not exist, I expect that the except statement executes. However, instead of except statement, the try statement fails with the error:

java.io.FileNotFoundException: GET ...
ErrorMessage=The specified path does not exist.

How to properly catch FileNotFoundException?

Fluxy
  • 2,838
  • 6
  • 34
  • 63

2 Answers2

2

here is alternative

import os
dir = "/dbfs/path_to_directory"

if not os.path.exists(dir):
  print('The path does not exist')
  raise IOError
Maria Nazari
  • 660
  • 1
  • 9
  • 27
1

This approach should work, and looks familiar with your code:

  try:
    dbutils.fs.ls(path)
    pass
  except Exception as e:
    if 'java.io.FileNotFoundException' in str(e):
      print('The path does not exist')
    else:
      raise
George Fandango
  • 121
  • 1
  • 12