简体   繁体   中英

Check if the path exists in Databricks

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 ?

here is alternative

import os
dir = "/dbfs/path_to_directory"

if not os.path.exists(dir):
  print('The path does not exist')
  raise IOError

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM