我正在尝试在 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")**
我希望有类似于上面代码片段的东西,但是我无法采用这种方法。我会请求一些帮助真的很感激
最佳答案
你不能直接排除 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')
关于apache-spark - 使用 pyspark 在数据 block 中实现 FileNotFound 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64808353/