Pra*_*nty 5 apache-spark apache-spark-sql pyspark databricks azure-databricks
我正在尝试使用 Databricks 中的 Pyspark 实现异常处理,其中我需要检查文件是否存在于源位置中。
df = spark.read.csv.option("inferschema", "true").load("mnt/pnt/abc.csv")
try:
df = open("abc.csv", "rt")
print("File opened")
except FileNotFoundError:
print("File does not exist")
except:
print("Other error")**
Run Code Online (Sandbox Code Playgroud)
我希望有类似上面的代码片段的东西,但是我无法采取这种方法。我想请求一些帮助,我将非常感激
小智 13
您不能直接 except java.io 错误,但是您可以执行以下操作:
def read_file(path):
try:
dbutils.fs.ls(path)
return spark.read.option("inferschema","true").csv(path)
except Exception as e:
if 'java.io.FileNotFoundException' in str(e):
print('File does not exists')
else:
print('Other error')
read_file('mnt/pnt/abc.csv')
Run Code Online (Sandbox Code Playgroud)