【问题标题】:Check if the path exists in Databricks检查路径是否存在于 Databricks 中
【发布时间】:2020-02-17 16:52:31
【问题描述】:

我尝试使用 Python 检查路径是否存在于 Databricks 中:

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

如果路径不存在,我希望except 语句会执行。 但是,try 语句不是 except 语句,而是失败并出现错误:

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

如何正确捕捉FileNotFoundException?

【问题讨论】:

    标签: python databricks azure-databricks dbutils


    【解决方案1】:

    这是另一种选择

    import os
    dir = "/dbfs/path_to_directory"
    
    if not os.path.exists(dir):
      print('The path does not exist')
      raise IOError
    

    【讨论】:

      【解决方案2】:

      这种方法应该可行,并且看起来对您的代码很熟悉:

        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
      

      【讨论】:

        猜你喜欢
        • 2019-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-01
        • 2023-03-17
        • 2020-05-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多